简体   繁体   中英

Split dataframe based on column values of another dataframe in python

I have the following dataframe:

Date    Country Type    Consumption
01/01/2019     Fr   IE  186
02/01/2019     Fr   IE  131
01/01/2019      Fr  SE  115
02/01/2019     Fr   SE  141
03/01/2019     Fr   SE  158
01/01/2019     Po   DK  208
01/01/2019     Po   IE  150
02/01/2019     Po   IE  136
01/01/2019    Po    SE  210
02/01/2019     Po   SE  195
03/01/2019     Po   SE  160
01/01/2019     Hk   DK  229
01/01/2019     Hk   IE  159
02/01/2019     Hk   IE  210
01/01/2019     Hk   SE  130
02/01/2019     Hk   SE  179
03/01/2019     Hk   SE  143

I want to split it into multiple dataframes by country & type. For example I want to have

df_1:

在此处输入图片说明

df_2:

在此处输入图片说明

df_3:

在此处输入图片说明

df_4:

在此处输入图片说明

& so on ...

I created another dataframe

df = pd.DataFrame({
"Country": ["Fr", "Po"],
"Type": ["IE", "SE"]})

because I only want to create new dataframes based on these values in "df"

Used the following code :

#create unique list of names

 UniqueNames = pd.unique(df[['Country','Type']].values.ravel())
 DataFrameDict = {elem : pd.DataFrame for elem in UniqueNames}

 for key in DataFrameDict.keys():
     DataFrameDict[key] = df3[:][df3.Country == key]

But this does not serve the purpose & I am getting dataframes with all type values.

How can this be achieved ?

I also tried the following code :

d = {}
for name, group in df3.groupby(['City','Type']):
    d['group_' + str(name)] = group

But the problem is that it creates dataframes for every unique combination of City & Type while I only need a few combination.

Also the dataframe names are like d["group_('Fr', 'IE')"] d["group_('Fr', 'SE')"]

Can I change these names to much simpler ones like Fr_IE Fr_SE because I need to run many other functions on each of these dataframes

Convert the dataframe with the desired values into a list of tuples to be able to loop and filter through it

tuples = [tuple(x) for x in df.values]

Finally, filter the original dataframe with each of the items in the list, here I print each of them but you might want to do something else...

for mytuple in tuples:
    print(original_df[(original_df['Country'] == mytuple[0]) & (original_df['Type'] == mytuple[1])])

To save each dataframe in a new variable you can do it with a list:

my_dfs = [df[(df['Country'] == mytuple[0]) & (df['Type'] == mytuple[1])] for mytuple in tuples]
for my_df in my_dfs:
    print(my_df)

Given that I understood the question correctly, if you just define the key dataframe df as you did below:

df = pd.DataFrame({
"Country": ["Fr", "Po"],
"Type": ["IE", "SE"]})

you are missing the other combinations like: ['Fr','SE'] and ['Po','IE'].

I solved the problem as below. Hope this helps:

import pandas as pd

# I put your original data in a file called data.txt
# and read it into a dataframe called df_data
df_data = pd.read_csv('data.txt', sep=',')
print(df_data)

# Creating a dataframe of all selected country and type pairs
df_temp = df_data.groupby(['Country', 'Type']).size().reset_index(name='Count')
df = df_temp[df_temp['Country'].isin(['Fr', 'Po']) & df_temp['Type'].isin(['IE', 'SE'])].drop('Count', axis=1)
print(df)

# Then loop through the tuples
tuples = [tuple(x) for x in df.values]
my_dfs = [df_data[(df_data['Country'] == mytuple[0]) & (df_data['Type'] == mytuple[1])] for mytuple in tuples]

for my_df in my_dfs:
    print(my_df)

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