我有这个数据帧:
大学id# | 年份 | >fin_element | 优先年份当前年份|
---|---|---|---|
1 | 2022 | 元素#1 | <1><2>|
1 | 2022 | 元素#23美元 | 5美元 |
1 | 2022 | 元素#32美元 | 0美元 |
试试这个:
out = (df
.set_index(['University_id', 'Year', 'fin_element'])
.stack()
.unstack(level=2)
.rename(columns={'level_2' : 'financial_period'})
.reset_index()
)
print(out)
fin_element University_id Year financial_period element #1 element #2 element #3
0 1 2022 prior_year $1 $3 $2
1 1 2022 current_year $2 $5 $0