I have the following sample dataframe:
I need to transform it to look like :
Pls note that:
- In the new column, the last 2 columns in original data frame have been replaced by the district variables of second last column of original df
- The original df was duplicating columns upto 'Year' column and the new df should have fewer rows because the new variables are now in a 'transposed manner'
What I had thought would work out never did. Because the rows are still duplicated:
print(PopRatesC.shape)
PopRates = PopRatesC.copy()
PopRates.rename(columns={'Value':'PopRate'}, inplace=True)
PopRates=pd.get_dummies(data=PopRates, columns=['Series'])
PopRates['Rural_PopRate']= PopRates['Series_Rural_Rate']*PopRates['PopRate']
PopRates['Urban_PopRate']= PopRates['Series_Urban_Rate']*PopRates['PopRate']
print(PopRates.shape)
PopRates[416:422]
Kindly explain the right approach I should use.