Home > OS >  Why can't I delete column/ field from imported CSV in Python?
Why can't I delete column/ field from imported CSV in Python?

Time:05-05

I merged the df you see below in another file, and when I imported it into the current file, I was met with a strange field, 'Unnamed: 0', that I hadn't seen before. It wasn't there back.

I have listed the column names, copied the field name exactly, and attempted to drop it but was unsuccessful.

enter image description here

Any input would be appreciated.

CodePudding user response:

Try this

df.drop('Unnamed: 0', axis = 1, inplace = True)

Always use inplace = True when you are changing something in place in a dataframe or try to store in the different dataframe like this if you are not changing the source dataframe,

new_df = df.drop('Unnamed: 0', axis = 1)

CodePudding user response:

try this :

df = pd.read_csv(path_to_data file_name, sep=',')
columns_to_drop = [column for column in df.columns if 'Unnamed:' in column]
df.drop(columns_to_drop, axis = 1, inplace = True)
  • Related