Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loss of data while writing a pandas dataframe to CSV using to_csv with index = False in python

I am trying to write a pandas dataframe that has just a single column with 2k rows. I want a csv file with all the 2k rows of dataframe without the index.

my_df = pd.DataFrame(test_label)
my_df.columns = ['Names']
my_df.to_csv('/Users/neeru/Desktop/dats/test1.csv', index = False, header = True)

While using the above code, I get around 1.7k rows in the saved csv file whereas when I make the index = True, I get all the 2k rows along with the index.

What should I do to get a csv file with a single column containing all rows without indexes?

P.S: I have just started using pandas.

like image 753
abi Avatar asked Oct 30 '22 09:10

abi


1 Answers

I just had the same problem.

I was saving the dataframe with the right arguments, like so:

df.to_csv(mask_path + df_train_label["id"][idx] + ".csv",
    index=False,
    header=False,)

My problem was that you also need to say header=None when you read the CSV file you just saved.

df_mask = pd.read_csv(mask_path + id + ".csv", header=None)

like image 74
Mymozaaa Avatar answered Nov 15 '22 05:11

Mymozaaa