-5

I want to import an excel where I want to keep just some columns. This is my code:

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")

I selected all the columns which I want it but 2 names of columns don't appear all time in the files which I have to import and these columns are 'usname' and 'sname'.

Cause of that I received an error ['usname','sname'] not in index

How can I do this ?

Thanks

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    Test if the columns are in `df` in the first place before trying to access them: `if 'usname' in df.columns: [...]` – GPhilo Sep 14 '21 at 09:30

1 Answers1

0

Source -- https://stackoverflow.com/a/38463068/14515824

You need to use df.reindex instead of df[[]]. I also have changed 'excel.xlsx' to r'excel.xlsx' to specify to only read the file.

An example:

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

Which in your code would be:

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")