简体   繁体   中英

how to create multiple dataframes from existing dataframe based on condition in Python

I have a dataframe as shown below. I would like to create multiple dataframes from this dataframe based on column ID.

df = pd.DataFrame(results)
print(df)

Result is:

       ID  NAME    COLOR
    0  01   ABC      RED                               
    1  01   ABC      ORANGE                  
    2  01   ABC      WHITE   
    3  02   DEF      RED
    4  02   DEF      PURPLE
    5  02   DEF      GREEN
    6  02   DEF      ORANGE
    7  02   DEF      BLACK
    8  03   GHI      RED
    9  03   GHI      BLACK
   10  03   GHI      GREEN
   11  03   GHI      ORANGE
   12  04   JKL      RED

Multiple Dataframes should come as shown below:I am not able to put it into python code, pls help.

           ID  NAME    COLOR
        0  01   ABC      RED                               
        1  01   ABC      ORANGE                  
        2  01   ABC      WHITE  



          ID  NAME    COLOR
       0  02   DEF      RED
       1  02   DEF      PURPLE
       2  02   DEF      GREEN
       3  02   DEF      ORANGE
       4  02   DEF      BLACK

          ID  NAME    COLOR
       0  03   GHI      RED
       1  03   GHI      BLACK
       2  03   GHI      GREEN
       3  03   GHI      ORANGE

           ID  NAME    COLOR
       0   04   JKL      RED 

you have to filter by column "NAME"

df_EDF = df[df.NAME == "EDF"]
df_GHI = df[df.NAME == "GHI"]

Sorry for the hard coded solution : here's my other solution :

import numpy as np 
import pandas as pd 


d = {'NAME': ["ABC", "ABC","ABC","GHI","GHI"], 'VALUE': [3, 4,5,6,7]}
df = pd.DataFrame(data=d)

# Get all unique names
cat = np.unique(df.NAME)

# create empty list of dataframes 
listOfDf = []

# for each unique name, create df_i with df filter by name, and append the list 
for i in cat:
    df_i = df[df.NAME == i].reset_index(drop = True)
    listOfDf.append(df_i)

# now you have a list of dataframe and can work with each element of the list 
    # as dataframe

print(listOfDf)

[  NAME  VALUE
0  ABC      3
1  ABC      4
2  ABC      5,   NAME  VALUE
0  GHI      6
1  GHI      7]


for x in range(len(listOfDf)):
    print(listOfDf[x])
    print("------")

  NAME  VALUE
0  ABC      3
1  ABC      4
2  ABC      5
------
  NAME  VALUE
0  GHI      6
1  GHI      7
------

you can do:

data_dict={'df'+str(i): grp for i , grp in df.groupby('ID')}

Which gives a dictionary:

{'df1':    ID NAME   COLOR
 0   1  ABC     RED
 1   1  ABC  ORANGE
 2   1  ABC   WHITE, 'df2':    ID NAME   COLOR
 3   2  DEF     RED
 4   2  DEF  PURPLE
 5   2  DEF   GREEN
 6   2  DEF  ORANGE
 7   2  DEF   BLACK, 'df3':     ID NAME   COLOR
 8    3  GHI     RED
 9    3  GHI   BLACK
 10   3  GHI   GREEN
 11   3  GHI  ORANGE, 'df4':     ID NAME COLOR
 12   4  JKL   RED}

Now just call each key to access every group of ID,

print(data_dict['df2'])

   ID NAME   COLOR
3   2  DEF     RED
4   2  DEF  PURPLE
5   2  DEF   GREEN
6   2  DEF  ORANGE
7   2  DEF   BLACK

You can try this out:

import pandas as pd
data= {'ID':[1,1,1,2,2,2,3,3,3,4], 'NAME':['ABC','ABC','ABC','DEF','DEF','DEF','GHI','GHI','GHI','JKL']}  
df = pd.DataFrame(data=data)

Solution 1 :

    myList=[]
    for id, df_id in df.groupby('ID'):
        print(df_id)
`       myList.append(df_id)
        Result:
         ID NAME
        0   1  ABC
        1   1  ABC
        2   1  ABC
           ID NAME
        3   2  DEF
        4   2  DEF
        5   2  DEF
           ID NAME
        6   3  GHI
        7   3  GHI
        8   3  GHI
           ID NAME
        9   4  JKL

You can access different dataframes like myList[2]

   ID   NAME
6   3   GHI
7   3   GHI
8   3   GHI

Solution 2:

{k: v for k, v in df.groupby('ID')}

    Result:
    {1:    ID NAME
     0   1  ABC
     1   1  ABC
     2   1  ABC, 2:    ID NAME
     3   2  DEF
     4   2  DEF
     5   2  DEF, 3:    ID NAME
     6   3  GHI
     7   3  GHI
     8   3  GHI, 4:    ID NAME
     9   4  JKL}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM