1

Initially, I have this dataframe:

dataframe 1

I save this as a csv file by using:

df.to_csv('Frequency.csv')

The problem lies with when I try to read the csv file again with:

pd.read_csv("Frequency.csv")

The dataframe then looks like this:

dataframe 2

Why is there an extra column added and why did the index change? I suppose it has something the do with the way how you should save the dataframe as a csv file, but I am not sure.

2 Answers 2

1

Use these to save and read:

#if you don't want to save the index column in the first place
df.to_csv('Frequency.csv', index=False) 
# drop the extra column if any while reading
pd.read_csv("Frequency.csv",index_col=0)

Example :

import pandas as pd

data = {
  "calories": [420, 380, 390],
  "duration": [50, 40, 45]
}
df1 = pd.DataFrame(data)

df1.to_csv('calories.csv', index=False)
pd.read_csv("calories.csv",index_col=0)

I've used the combination of these 2 given below because my jupyter notebook adds index on it's own while reading even if I use index=False while saving. So I find the combo of these 2 as a full proof method.

df1.to_csv('calories.csv', index=False)
pd.read_csv("calories.csv",index_col=0)
Sign up to request clarification or add additional context in comments.

2 Comments

When I use df.to_csv('frequency', index=False) the original index which contains 'measure' and 'frequency of item' disappears. So eventually I end up with a dataframe with the index [0,1] and the columns [0,1].
Then reset the index before you save with .reset_index().
0

It's writing the index into the csv, so then when you load it, it's an unnamed column. You can get around it by writing the csv like this.

df.to_csv('Frequency.csv', index=False)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.