I have a pandas multiindex data frame with four index levels. I am trying to divide a slice of this dataframe by another slice of the same dataframe.
import pandas as pd
df = pd.DataFrame(
data={"data_provider": ["prov_a", "prov_a", "prov_a", "prov_a", "prov_a", "prov_a"],
"indicator": ["ind_a", "ind_a", "ind_a", "ind_b", "ind_b", "ind_b"],
"unit": ["EUR", "EUR", "EUR", "EUR", "EUR", "EUR"],
"year": ["2017", "2018","2019", "2017","2018","2019"],
"country1": [1, 2, 3, 2, 4, 6],
"country2": [4, 5, 6, 40, 50, 60]}
)
df = df.set_index(["data_provider", "indicator", "unit", "year"], drop=True)
print(df.loc[(slice(None), ["ind_a"]), :] / df.loc[(slice(None), ["ind_b"]), :])
Although the individual slices produce a valid slice of df, this simple division leads to all NaN. If I were to drop the first index level and perform the same slicing and division operation, I do get correct results. However, the indicator index-level will then be removed, which makes sense.
df1.droplevel(0)
print(df.loc["ind_a", :] / df.loc["ind_b", :])
At the end of the day, I would like to append the result of the division to the existing df dataframe. I need to assign the first two levels of the multiindex. Something like data_provider="prov_a" and indicator="ind_c". How can I do this?