How do I turn pandas DataFrame groupby results into a DataFrame?

CWeeks

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?

jezrael

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to group dataframe rows into list in pandas groupby

From Java

How to GroupBy a Dataframe in Pandas and keep Columns

From Dev

How to turn pandas dataframe row into ordereddict fast

From Dev

How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

From Dev

pandas dataframe groupby summation

From Dev

How do I reshape or pivot a DataFrame in Pandas

From Dev

How do I write a pandas dataframe to Vertica?

From Dev

How do I put a series (such as) the result of a pandas groupby.apply(f) into a new column of the dataframe?

From Dev

How do I columnwise reduce a pandas dataframe?

From Dev

How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

From Dev

append pandas.DataFrame.GroupBy results into another dataframe

From Dev

How do I turn a Pandas DataFrame object with 1 main column into a Pandas Series with the index column from the original DataFrame

From Dev

how to groupby pandas dataframe on some condition

From Dev

How do I turn an array of column names into a pandas Dataframe?

From Dev

groupby pandas dataframe and create another dataframe which represents the groupby results horizontally

From Dev

How do I turn a dataframe into a series of lists?

From Dev

how to pivot a Dataframe groupby results

From Dev

How do I turn an event dataframe with "end" and "start" rows into a regrouped by event dataframe?

From Dev

How to groupby consecutive values in pandas DataFrame

From Dev

How do I turn a dataframe into a series of lists?

From Dev

How to turn pandas dataframe row into ordereddict fast

From Dev

How to groupby pandas DataFrame by customized function

From Dev

append pandas.DataFrame.GroupBy results into another dataframe

From Dev

groupby pandas dataframe and create another dataframe which represents the groupby results horizontally

From Dev

How to groupby with certain condition in pandas dataframe

From Dev

merge values of groupby results with dataframe in new column Python Pandas

From Dev

How do i sort the values in a dataframe in pandas?

From Dev

How can I turn picture to structured dataframe?

From Dev

How to do sorting after groupby and aggregation on a Pandas Dataframe

Related Related

  1. 1

    How to group dataframe rows into list in pandas groupby

  2. 2

    How to GroupBy a Dataframe in Pandas and keep Columns

  3. 3

    How to turn pandas dataframe row into ordereddict fast

  4. 4

    How do I get the first timestamp (index) of a group when applying groupby to a python pandas dataframe?

  5. 5

    pandas dataframe groupby summation

  6. 6

    How do I reshape or pivot a DataFrame in Pandas

  7. 7

    How do I write a pandas dataframe to Vertica?

  8. 8

    How do I put a series (such as) the result of a pandas groupby.apply(f) into a new column of the dataframe?

  9. 9

    How do I columnwise reduce a pandas dataframe?

  10. 10

    How to efficiently columnize (=pivoting) pandas DataFrame (with groupby)?

  11. 11

    append pandas.DataFrame.GroupBy results into another dataframe

  12. 12

    How do I turn a Pandas DataFrame object with 1 main column into a Pandas Series with the index column from the original DataFrame

  13. 13

    how to groupby pandas dataframe on some condition

  14. 14

    How do I turn an array of column names into a pandas Dataframe?

  15. 15

    groupby pandas dataframe and create another dataframe which represents the groupby results horizontally

  16. 16

    How do I turn a dataframe into a series of lists?

  17. 17

    how to pivot a Dataframe groupby results

  18. 18

    How do I turn an event dataframe with "end" and "start" rows into a regrouped by event dataframe?

  19. 19

    How to groupby consecutive values in pandas DataFrame

  20. 20

    How do I turn a dataframe into a series of lists?

  21. 21

    How to turn pandas dataframe row into ordereddict fast

  22. 22

    How to groupby pandas DataFrame by customized function

  23. 23

    append pandas.DataFrame.GroupBy results into another dataframe

  24. 24

    groupby pandas dataframe and create another dataframe which represents the groupby results horizontally

  25. 25

    How to groupby with certain condition in pandas dataframe

  26. 26

    merge values of groupby results with dataframe in new column Python Pandas

  27. 27

    How do i sort the values in a dataframe in pandas?

  28. 28

    How can I turn picture to structured dataframe?

  29. 29

    How to do sorting after groupby and aggregation on a Pandas Dataframe

HotTag

Archive