【问题标题】:Pandas dataframe熊猫数据框
【发布时间】:2021-11-09 11:43:28
【问题描述】:

我想导入一个只保留一些列的 Excel。 这是我的代码:

df=pd.read_excel(file_location_PDD)
 col=df[['hkont','dmbtr','belnr','monat','gjahr','budat','shkzg','shkzg','usname','sname','dmsol','dmhab']]
 print(col)
col.to_excel("JETNEW.xlsx")

我选择了我想要的所有列,但是我必须导入的文件中并没有出现 2 个列的名称,这些列是“usname”和“sname”。

因为我收到一个错误 ['usname','sname'] not in index

我该怎么做?

谢谢

【问题讨论】:

  • 在尝试访问它们之前首先测试列是否在df中:if 'usname' in df.columns: [...]

标签: python pandas


【解决方案1】:

来源——https://stackoverflow.com/a/38463068/14515824

您需要使用df.reindex 而不是df[[]]。我还将'excel.xlsx' 更改为r'excel.xlsx' 以指定仅读取文件。

一个例子:

df.reindex(columns=['a','b','c'])

在您的代码中将是:

file_location_PDD = r'excel.xlsx'
df = pd.read_excel(file_location_PDD)
col = df.reindex(columns=['hkont','dmbtr','belnr','monat','gjahr','budat','shkzg','shkzg','usname','sname','dmsol','dmhab'])
print(col)
col.to_excel("output.xlsx")

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多