removing columns with pandas from csv - not found in axis

Viewed 72
2 Answers

Mustansir is right, you can try changing your code with

import pandas as pd
df.drop("First Invoice #", axis = 1, inplace = True)

If you refer to the pandas documentation for DataFrame.drop, you can see that axis = 0 refers to the index (row) while axis = 1 refers to the columns, and by default, the function is dropping by the index.

The pd.drop() function is preferred as it also deletes the instance instead of the just the name.

Your problem is that your data is separated by semicolon instead of comma. Use:

import pandas as pd
df = pd.read_csv('Test.csv', sep=';')

Demonstration:

import pandas as pd
df = pd.read_csv('Test.csv', sep=';')
df.drop("First Invoice #", axis = 1, inplace= True)
df

Output:

enter image description here

You can use del too:

del df["First Invoice #"]
Related