Here is what I have done:
df = pd.DataFrame(data={'in':[2, 2, 3, 4], 'out':[2,2,4,4]})
in_out=df.groupby(['in','out']).size()
I got results:
in out
2 2 2
3 4 1
4 4 1
dtype: int64
How do I turn the results into a dataframe with columns in, out and count?
Use reset_index
with parameter name
:
df = pd.DataFrame(data={'in':[2, 2, 3, 4], 'out':[2,2,4,4]})
in_out=df.groupby(['in','out']).size().reset_index(name='count')
print in_out
in out count
0 2 2 2
1 3 4 1
2 4 4 1
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments