'removing columns with pandas from csv - not found in axis

I'm trying to remove 1 column from .csv but I'm receiving an error.

import pandas as pd
df.drop("First Invoice #", axis = 1, inplace= True)
KeyError: "['First Invoice #'] not found in axis"

Here you find file .csv https://drive.google.com/file/d/1Q1_PByc-xaFOc47Fg9bGLOj6jsMGIxJx/view?usp=sharing

Do you have any ideas?

Thank you for your help Angelo



Solution 1:[1]

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.

Solution 2:[2]

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 #"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2