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

pandas create new column based on multiple condition
create new data frames from existing dataframe based on unique column values
create new dataframe from existing dataframe pandas
pandas select rows by multiple conditions
split dataframe into multiple dataframes pandas
pandas split dataframe by condition
pandas select columns by condition
pandas create new dataframe based on other columns

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 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

Create new data frames from existing data frame based on unique , You can groupby company_id column and convert its result into a dictionary of DataFrames: import pandas as pd df = pd.DataFrame({� Dataframe.merge() In Python’s Pandas Library Dataframe class provides a function to merge Dataframes i.e. DataFrame.merge(right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None) It accepts a hell lot of arguments.

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
------

Splitting Pandas dataframe into multiple dataframes based on , Splitting Pandas dataframe into multiple dataframes based on condition in column python split dataframes String into columns using regex in pandas DataFrame; Create a new column in Pandas DataFrame based on the existing columns;� 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

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}

How to generate a new dataframe in python by considering , I have two dataframes and wish to generate third dataframe as per the conditions mentioned, the scenario is explained below: dataframe 1:. If so, I’ll show you two different methods to create Pandas DataFrame: By typing the values in Python itself to create the DataFrame; By importing the values from a file (such as an Excel file), and then creating the DataFrame in Python based on the values imported; Method 1: typing values in Python to create Pandas DataFrame. To create

Python, Return the Index label if some condition is satisfied over a column in Pandas Dataframe � Create a new column in Pandas DataFrame based on the existing� Append column to dataFrame using assign () function. In Python, Pandas Library provides a function to add columns i.e. It accepts a keyword & value pairs, where a keyword is column name and value is either list / series or a callable entry. It returns a new dataframe and doesn’t modify the current dataframe.

Selecting rows in pandas DataFrame based on conditions , Django Tutorial � Defaultdict in Python � Pandas Tutorial � Max Heap in Python � Django Selecting rows in pandas DataFrame based on conditions. Let's see how to Select rows based on some conditions in Pandas DataFrame. create a dataframe Selecting rows based on multiple column conditions using '&' operator. Takuya Iwanaga has it right. I’ll just add a function that explicitly returns two DataFrames: [code]In [1]: import numpy as np In [2]: import pandas as pd In [3

Add a Column in a Pandas DataFrame Based on an If-Else Condition, Need to add a column to your pandas DataFrame based on values found elsewhere in When we're doing data analysis with Python, we might sometimes want to add a column to a pandas our true goal here: adding columns to pandas DataFrames based on conditional statements about values in our existing columns. In this article, we will cover various methods to filter pandas dataframe in Python. Data Filtering is one of the most frequent data manipulation operation. It is similar to WHERE clause in SQL or you must have used filter in MS Excel for selecting specific rows based on some conditions.

Comments
  • Does my answer work?
  • Yes It worked..Thanks
  • Great..!! would you consider accepting an answer please then. :) (Just click on the tick mark in grey to the left of the answer). Thanks
  • filter on hard coding is not possible since i have dataframe with lots of data.