0

I'm trying to export my df to a .csv file. The df has just two columns of data: the image name (.jpg), and the 'value_counts' of how many times that .jpg name occurs in the 'concat_Xenos.csv' file, i.e:

M116_13331848_13109013329679.jpg    19
M116_13331848_13109013316679.jpg    14
M116_13331848_13109013350679.jpg    12
M116_13331848_13109013332679.jpg    11
etc. etc. etc....

However, whenever I export the df, the .csv file only displayes the 'value_counts' column. How do I modify this?

My code is as follows:

concat_Xenos = r'C:\file_path\concat_Xenos.csv'

df = pd.read_csv(concat_Xenos, header=None, index_col=False)[0]
counts = df.value_counts()

export_csv = counts.to_csv (r'C:\file_path\concat_Xenos_valuecounts.csv', index=None, header=False)

Thanks! If any clarification is needed please ask :)

R

1 Answer 1

3

This is because the first column is set as index.

Use index=True:

export_csv = counts.to_csv (r'C:\file_path\concat_Xenos_valuecounts.csv', index=True, header=False)

or you can reset your index before exporting.

counts.reset_index(inplace=True)
Sign up to request clarification or add additional context in comments.

1 Comment

Simple as that! Thank you @Rakesh , that has worked. However, now the csv is also counting the header in the 'value_counts' command, and is including the row 'filename, 1' at the bottom of the new csv file. How do I prevent my code from including headers in the counts? In line two of my script above changing the 'header=None' to 'True' or 'False' just returns errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.