当“carries.xlsx”电子表格中的单元格为空时,我试图删除一行,并将所有数据(空行除外)发送到“destination.xlsx”电子表格。下面的代码是我走了多远。但是,它仍然不会删除基于卡路里列的空值行
这是数据集:
如何开发代码来解决此问题
import pandas as pd
FileName = 'calories.xlsx'
SheetName = pd.read_excel(FileName, sheet_name = 'Sheet1')
df = SheetName
print(df)
ListCalories = ['Calories']
print(ListCalories)
for Cell in ListCalories:
if Cell == 'NaN':
df.drop[Cell]
print(df)
df.to_excel('destination.xlsx')
创建虚拟数据
打印数据框
如果缺少卡路里值,则删除行
结果
另存为excel
您的
ListCalories
只包含一个元素Calories
,我假设这是一个输入错误。 你可能想做的是https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.notna.html
相关问题 更多 >
编程相关推荐