Pandas dataframe groupby make a list or array of a column

burcak
:
import pandas as pd
import numpy as np

df = {'a': ['aa', 'aa', 'aa', 'aaa', 'aaa'], 
      'b':['bb', 'bb', 'bb', 'bbb', 'bbb'], 
      'c':[10,20,30,100,200]}

df = pd.DataFrame(data=df)

my_dict=df.groupby(['a', 'b'])['c'].apply(np.hstack).to_dict()

gives the following dictionary

>>> my_dict
{('aa', 'bb'): array([10, 20, 30]), ('aaa', 'bbb'): array([100, 200])}

Is there a faster/efficient way of doing this other than using apply?

jezrael
:

Use dictionary comprehension:

my_dict= {k:np.hstack(v) for k, v in df.groupby(['a', 'b'])['c']}
print (my_dict)
{('aa', 'bb'): array([10, 20, 30]), ('aaa', 'bbb'): array([100, 200])}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas add column to groupby dataframe

From Dev

Pandas dataframe groupby remove column

From Dev

pandas dataframe: subset by column + groupby another column

From Dev

Pandas stack/groupby to make a new dataframe

From Dev

Pandas: groupby and make a new column by concatenating the result

From Dev

Pandas : Assign result of groupby to dataframe to a new column

From Dev

Pandas multiplying a dataframe column with groupby result

From Dev

Advanced groupby column creation in pandas Dataframe

From Java

How to group dataframe rows into list in pandas groupby

From Dev

Groupby items within a list in a Pandas DataFrame

From Dev

Pandas DataFrame column values in to list

From Java

Extract List in Column Pandas Dataframe

From Dev

Pandas: Merge a dataframe column to a list

From Dev

Convert List to Pandas Dataframe Column

From Dev

Storing list in a pandas DataFrame column

From Dev

pandas groupby column to list and keep certain values

From Dev

Is it possible to filter Pandas DataFrame column if column is a list?

From Dev

how to create a pandas DataFrame by combining a list of column_names and a numpy array, and then adding more column(s)?

From Dev

Make column from pandas dataframe index

From Dev

Pandas make a new dataframe with the old column names

From Dev

Combine list element by column name to make a dataframe

From Dev

Combine list element by column name to make a dataframe

From Dev

Pandas reformatting table to make certain rows to column after groupby

From Dev

Retaining a column with all strings during groupby on a pandas dataframe

From Java

Pandas transform dataframe using groupby when count of a string in a column is maximum

From Dev

How to get a new dataframe with column count out of groupby function in pandas?

From Dev

Select CONSECUTIVE rows from a DataFrame based on values in a column in Pandas with Groupby

From Dev

Pandas: Add new column with several values to groupby dataframe

From Dev

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

Related Related

  1. 1

    pandas add column to groupby dataframe

  2. 2

    Pandas dataframe groupby remove column

  3. 3

    pandas dataframe: subset by column + groupby another column

  4. 4

    Pandas stack/groupby to make a new dataframe

  5. 5

    Pandas: groupby and make a new column by concatenating the result

  6. 6

    Pandas : Assign result of groupby to dataframe to a new column

  7. 7

    Pandas multiplying a dataframe column with groupby result

  8. 8

    Advanced groupby column creation in pandas Dataframe

  9. 9

    How to group dataframe rows into list in pandas groupby

  10. 10

    Groupby items within a list in a Pandas DataFrame

  11. 11

    Pandas DataFrame column values in to list

  12. 12

    Extract List in Column Pandas Dataframe

  13. 13

    Pandas: Merge a dataframe column to a list

  14. 14

    Convert List to Pandas Dataframe Column

  15. 15

    Storing list in a pandas DataFrame column

  16. 16

    pandas groupby column to list and keep certain values

  17. 17

    Is it possible to filter Pandas DataFrame column if column is a list?

  18. 18

    how to create a pandas DataFrame by combining a list of column_names and a numpy array, and then adding more column(s)?

  19. 19

    Make column from pandas dataframe index

  20. 20

    Pandas make a new dataframe with the old column names

  21. 21

    Combine list element by column name to make a dataframe

  22. 22

    Combine list element by column name to make a dataframe

  23. 23

    Pandas reformatting table to make certain rows to column after groupby

  24. 24

    Retaining a column with all strings during groupby on a pandas dataframe

  25. 25

    Pandas transform dataframe using groupby when count of a string in a column is maximum

  26. 26

    How to get a new dataframe with column count out of groupby function in pandas?

  27. 27

    Select CONSECUTIVE rows from a DataFrame based on values in a column in Pandas with Groupby

  28. 28

    Pandas: Add new column with several values to groupby dataframe

  29. 29

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

HotTag

Archive