Deleting a Column in a csv or Excel file using Pandas

I am trying to delete a column from my csv file (column 'A' called Film Number) but have tried numerous variations of code and while it deletes the column in the dataFrame it doesn't do so in the actual excel or csv file, it just deletes the column name but not the entire column. I'm using PyCharm. Here's the code. Any insights or help would be greatly appreciated.

import pandas as pd

# create a dataframe from the csv file and read the file
master_df = pd.read_csv(Master IMDB File Practice.csv)
master_df.head()
master_df.columns
# print(master_df.columns)

# To remove the column named Film Number

master_df.drop([Film Number], axis=1, inplace=True)
print(master_df.columns)

# save as an excel file
master_df.to_excel(Master IMDB File Practice New.xlsx)

# save as a csv file
# master_df.to_csv(Master IMDB File Practice New.csv)

This is what it prints, so it removed the Film Number column here.

Highlighted is the Excel file I saved it to in the last step, notice it only deleted the Film Number column name but not the entire column. I also tried saving it as a csv but I have the same issue.

Below is another version of the code where I got the same result - deleted column name but not the entire column when I opened the Excel file

import pandas as pd

# create a dataframe from the csv file and read in the file
df = pd.read_csv('Master IMDB File Practice.csv')
df.head()

# To delete the Film Number column
df.drop(['Film Number'], axis=1, inplace=True)
print(df)

# save as an excel file
df.to_excel(Master IMDB File Practice New.xlsx)

Topic data excel pandas python data-cleaning

Category Data Science


The code master_df.drop(["Film Number"], axis=1, inplace=True) you have written is right. What is happening is like you have removed the column perfectly but while converting to csv file or excel file the index column (whatever column you have mentioned with values like 0,1,2,3) get added in the output so please replace one more argument index=False in the master_df.to_csv("Master IMDB File Practice New.csv") line as master_df.to_csv("Master IMDB File Practice New.csv", index=False).

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.