我有來自一長串字典的以下資料框(示例如下:
final_list = [{'name': 'total_video_followers', 'period': 'lifetime', 'values': [{'value': 10}], 'title': None, 'description': None, 'id': '5400'}, {'name': .... etc}]
它在使用時創建的資料框
df_list = pd.DataFrame(final_list)
| 指數 | 姓名 | 時期 | 價值觀 | ID |
|---|---|---|---|---|
| 0 | total_video_followers | 壽命 | [{'價值':10}] | 5400 |
| 1 | total_video_followers_unique | 壽命 | [{'價值':1}] | 5400 |
但我想更改軸,所以名稱列應該是標題,而且 id 也應該是標題
| 指數 | total_video_followers | total_video_followers_unique | ID | 時期 |
|---|---|---|---|---|
| 0 | [{'價值':10}] | [{'價值':1}] | 5400 | 壽命 |
我嘗試了樞軸和轉置,但沒有奏效:
pivoted = df_list.pivot(columns='name').reset_index()
transpose = df_list.T
當我嘗試最后一行時,轉置,標題是 0、1、2....
uj5u.com熱心網友回復:
考慮旋轉您的資料框
df.pivot(['id', 'period'], 'name', 'values').reset_index()
name id period total_video_followers total_video_followers_unique
0 5400 lifetime [{'value': 10}] [{'value': 1}]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/491328.html
