'How to create a dataframe matrix from other data frames

I have 2 data frames, from which I want to create a third data frame(country) from data from the 2 data frames. Below the data:

Indicator 1

    country 2001    2002    2003    2004    2005    2006    2007    2008    2009    2010

1   Angola  200.0   193.0   185.0   176.0   167.0   157.0   148.0   138.0   129.0   120.0
2   Albania 24.5    23.1    21.8    20.4    19.2    17.9    16.7    15.5    14.4    13.3
195 Zambia  153.0   142.0   130.0   119.0   110.0   101.0   95.4    90.4    85.1    80.3



Indicator2

    country 2001    2002    2003    2004    2005    2006    2007    2008    2009    2010

1   Angola  53.4    54.5    55.1    55.5    56.4    57.0    58.0    58.8    59.5    60.2
2   Albania 76.0    75.9    75.6    75.8    76.2    76.9    77.5    77.6    78.0    78.1
193 Zambia  45.2    45.9    46.6    47.7    48.7    50.0    51.9    54.1    55.7    56.5

I need to create a new data frame for each country like below

Angloa
           2001 2002    2003    2004    2005    2006    2007    2008    2009    2010

Indicator1 200.0    193.0   185.0   176.0   167.0   157.0   148.0   138.0   129.0   120.0

Indicator2 53.4 54.5    55.1    55.5    56.4    57.0    58.0    58.8    59.5    60.2

I need to know the code for creating this new data frame



Solution 1:[1]

What you asked can be done this way :

# Setting up DataFrames
indicator1 = pd.DataFrame({
    'country': ['Angola', 'Albania', 'Zambia'],
    '2001': ["200.0", "24.5", "153.0"],
    '2002': ["193.0", "23.1", "142.0"]
})
indicator2 = pd.DataFrame({
    'country': ['Angola', 'Albania', 'Zambia'],
    '2001': ["53.4", "76.0", "45.2"],
    '2002': ["54.5", "75.9", "45.9"]
})

# For each country
for index, row in indicator1.iterrows():
    # create a new variable with the country as name
    globals()[f"{row['country']}"] = {}
    # For each column of your 2 dataframes
    for key, value in indicator1.iteritems():
        if key != 'country':
            globals()[f"{row['country']}"][key] = [row[key], indicator2.iloc[indicator2[indicator2['country'] == row[
                'country']].index.values[0]][key]]

    globals()[f"{row['country']}"] = pd.DataFrame(globals()[f"{row['country']}"])

I've only did it with an extract from your data, but it can be generalised. I'm not sure saving the newly created DataFrame like this is the best way, but I had no variable idea so I let you worry about this.

print(Angola)
# Output :
    2001   2002
0  200.0  193.0
1   53.4   54.5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1