Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas value counts save output to file

Tags:

pandas

numpy

I use pandas's value_counts() method to get the number of times each value in a column appears. Although the output looks like what I expected, attempting to save it using numpy savetxt or pandas to_csv returns only one column (with counts). I'd like to be able to save both.

like image 780
mannaroth Avatar asked Aug 08 '17 20:08

mannaroth


1 Answers

One way would be to use reset_index and then to_csv

df['key'].value_counts().reset_index().to_csv('df.csv')
like image 113
Vaishali Avatar answered Sep 28 '22 11:09

Vaishali