1 import pandas as pd 2 3 # 加载数据 4 detail = pd.read_excel("./meal_order_detail.xlsx") 5 print("detail: \n", detail) 6 print("detail的列名称: \n", detail.columns) 7 8 # 1、删除法 9 # 先进性判断,哪些列都是空的 10 drop_list = [] 11 for column in detail.columns: 12 print(column) 13 # 统计每一列非空数据的数量 14 res = detail.loc[:, column].count() 15 print("res: \n", res) 16 if res == 0: 17 drop_list.append(column) 18 19 print(drop_list) 20 21 # 再进行删除: 22 detail.drop(labels=drop_list, axis=1, inplace=True) 23 print("删除全部为空列之后的结果: \n" ,detail.shape) 24 25 # 2、保留法 26 # 先进性判断,哪些列不全为空 27 save_list = [] 28 for column in detail.columns: 29 print("detail.columns: \n", detail.columns) 30 print("columns: \n", column) 31 # 统计每一列非空数据的数量 32 res = detail.loc[:, column].count() 33 # print("res: \n", res) 34 if res != 0: 35 save_list.append(column) 36 37 print(save_list) 38 39 # 将不全为空的列的数据重新赋值给detail 40 detail = detail.loc[:, save_list] 41 42 print("datail: \n", detail) 43 print("保留全部为空列之后的结果: \n", detail.shape)
原文地址:https://www.cnblogs.com/Tree0108/p/12116070.html
时间: 2024-11-04 11:55:01