gpt4 book ai didi

python - 我想将数据帧转换为特定格式的数据帧或列表列表

转载 作者:行者123 更新时间:2023-12-01 06:57:49 25 4
gpt4 key购买 nike

这是数据框

Year    Category    Performance
2014 Sales 1000
2014 Expenses 400
2014 Profit 200
2015 Sales 1170
2015 Expenses 460
2015 Profit 250
2016 Sales 660
2016 Expenses 1120
2016 Profit 300

这是预期的输出

 [          
          ['Year', 'Sales', 'Expenses', 'Profit'],
          ['2014', 1000, 400, 200],
          ['2015', 1170, 460, 250],
          ['2016', 660, 1120, 300],
        ]

或者您是否可以帮我将此数据框转换为此

Year Sales Expenses Profit      
2014 1000 400 200
2015 1170 460 250
2016 660 1120 300

最佳答案

df = pd.pivot_table(df, index='Year', columns='Category', values='Performence').reset_index()

关于python - 我想将数据帧转换为特定格式的数据帧或列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58730933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com