Home > Net >  Looping over unique ID and plotting the variables of those unique ID individually
Looping over unique ID and plotting the variables of those unique ID individually

Time:10-03

I have the panel dataset of multiple countries with ISO3 based IDs and their respective macroeconomic indicators. The data looks like the following, where the number of unique IDs equal to 142 (countries):

dfx = pd.DataFrame({'ID': ['a', 'a', 'd', 'd', 'e', 'e', 'g'],
                    'year': [1999,2000,1999,2000,1999,2000,2000],
                    'x1': [1,2,3,4,5,6,7],
                    'x2': [8,9,10,11,12,13,14]}) 
print(dfx)

  ID  year  x1  x2
0  a  1999   1   8
1  a  2000   2   9
2  d  1999   3  10
3  d  2000   4  11
4  e  1999   5  12
5  e  2000   6  13
6  g  2000   7  14

I want to split dataset by unique country IDs, store it and do the plotting and running a regression on each country based on its macroeconomic variables with a certain loop. For example, I have been able to split the dataframe to the following shape, where each df belongs to a particular country, e.g. df1 is Angola:

for j, (ID, subdf) in enumerate(dfx.groupby('ID'), 1):
    locals()[f'df{j}'] = subdf
print(df1)
print(df2)
print(df3)
print(df4)

 ID  year  x1  x2
0  a  1999   1   8
1  a  2000   2   9
  ID  year  x1  x2
2  d  1999   3  10
3  d  2000   4  11
  ID  year  x1  x2
4  e  1999   5  12
5  e  2000   6  13
  ID  year  x1  x2
6  g  2000   7  14

And, having those dataframes, I want to construct a loop, using those dataframes (df1, df2, df3, df4 and so on till df142) to iterate over each country to plot their macro variables (x1 and x2) separately.

Thank you in advance!

CodePudding user response:

You could split them into groups by ID then store them in a dictionary which you can loop over to make plots based on the single groups.

In [24]: groups = {key: df.loc[value] for key, value in df.groupby("ID").groups.items()}
In [26]: for key, group in groups.items():
    ...:     print(group)
    ...: 
  ID  year  x1  x2
0  a  1999   1   8
1  a  2000   2   9
  ID  year  x1  x2
2  d  1999   3  10
3  d  2000   4  11
  ID  year  x1  x2
4  e  1999   5  12
5  e  2000   6  13
  ID  year  x1  x2
6  g  2000   7  14

Dictionary of groups:

{'a':   ID  year  x1  x2
 0  a  1999   1   8
 1  a  2000   2   9,
 'd':   ID  year  x1  x2
 2  d  1999   3  10
 3  d  2000   4  11,
 'e':   ID  year  x1  x2
 4  e  1999   5  12
 5  e  2000   6  13,
 'g':   ID  year  x1  x2
 6  g  2000   7  14}
  • Related