I was just wondering is it possible to get the output of a pandas .value_counts() to be a new dataframe? It is typically output in a couple of columns looking like a table, but something like the following doesn't work:
new_df = df['col'].value_counts()
And a related question (which is in part why I want to do the above) is I am struggling with using the output of df['col'].value_counts()
.
If your data looks like this:
Num, Let
0,A
1,A
2,B
Then you can run
counts = df['Let'].value_counts()
print counts['A']
which prints 2. And I could assign this value of 2 to a variable, e.g. max_let=counts['A']
. If I wanted to output the max I could also do something like counts.head(1)
I believe. But what if I wanted to assign the string 'A' to a variable aswell (that is why I wanted the extra dataframe), is that possible? And is there a way to set max_let
to take the value of whatever comes out without having to specify 'A' (as I can't seem to do it using counts.head(1)
?
Hopefully that wasn't too convoluted.