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



