'Why can't I delete column/ field from imported CSV in Python?
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.
Any input would be appreciated.
Solution 1:[1]
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)
Solution 2:[2]
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)
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 | Thirunaavukkarasu M |
| Solution 2 | DataSciRookie |

