Reputation: 109
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.
Upvotes: 5
Views: 4359
Reputation: 474
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)
Upvotes: 2