Python Pandas .str.split() creates an extra column that can't be dropped

Question:

I’m using the pandas split function to create new columns from an existing one. All of that works fine and I get my expected columns created. The issue is that it is creating an additional column in the exported csv file. So, it says there are 3 columns when there are actually 4.

I’ve tried various functions to drop that column, but it isn’t recognized as part of the data frame so it can’t be successfully removed.

Hopefully someone has had this issue and can offer a possible solution.

[example of the csv data frame output with the unnecessary column added]

1

Asked By: cjb10

||

Answers:

The column A doesn’t come from split but it’s the index of your actual dataframe by default. You can change that by setting index=False in df.to_csv:

df.to_csv('{PATH}.csv', index=False)
Answered By: Nuri Taş
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.