亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 Pandas 中的數據框從迭代列表轉換為適當的列和行

將 Pandas 中的數據框從迭代列表轉換為適當的列和行

墨色風雨 2021-09-02 20:09:53
我有一個看起來像的數據框Country | IndicatorName | ValueSpain   | Indicator1    | 3Spain   | Indicator2    | 4Germany | Indicator16   | 24......我想將它轉換成一個帶有 IndicatorName 列、Country 行和 Value 交叉點的數據框Country | Indicator 1 | Indicator 2 | Indicator 3 | ......Spain   |     3       |     4       |   16        | ......Germany |     23      |     232     |   232       | .............我正在嘗試通過 groupby(["IndicatorName","Value"]) 但不知道如何繼續import pandas as pdindicators = pd.read_csv("Indicators.csv")indicators.groupbby(["IndicatorName","Value"]).....有沒有合適的方法來處理這個問題還是需要通過迭代來完成?
查看完整描述

1 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

我不確定初始 df 格式,因為所需的 df 似乎具有不同的值。


下面的有用嗎?


df = pd.DataFrame({'Country' : ['Spain', 'Spain', 'Germany'],

                   'IndicatorName':['Indicator1', 'Indicator2', 'Indicator16'],

                  'Value':[3, 4, 24]

                  })



df.pivot(index = 'Country', columns='IndicatorName', values='Value').fillna(0)



IndicatorName   Indicator1  Indicator16     Indicator2

    Country             

    Germany            0.0        24.0              0.0

    Spain              3.0         0.0              4.0


查看完整回答
反對 回復 2021-09-02
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號