0

Could someone help me transform the below dataframe

import pandas as pd
df1 = pd.DataFrame({'A':[1,1,1], 'B':[2,2,2], 'C':['1y', '3y', '7y'], 'D':[111,222,333], 'E':[-111,-222,-333]})
print(df1)

into this form:

df2 = pd.DataFrame({'A':[1], 'B':[2], 'D_1y':[111], 'D_3y':[222], 'D_7y':[333], 'E_1y':[-111], 'E_3y':[-222], 'E_7y':[-333]})
print(df2)

This is a small sample from a huge dataframe, I have several columns like 'D' and 'E' all of which are linked to column 'C'.

user2696565
  • 587
  • 1
  • 8
  • 17

0 Answers0