我有一個熊貓資料框,看起來像
df =
Index1 Index2 Index3 column1 column2
i11 i12 i13 2 5
i11 i12 i23 3 8
i21 i22 i23 4 5
如何將其轉換為字典串列,鍵為Index3, column1, column2和值在相應的單元格中。
因此,預期輸出:
[[{Index3: i13, column1: 2, column2: 5}, {Index3: i23, column1: 3, column2: 8}], [{Index3: i23, column1: 4, column2: 5}]]
請注意,相同的價值觀Index1和Index2形式1內部串列和值將不再重復。
uj5u.com熱心網友回復:
d = {'Index1': ["i11", "i12", "i13"],
'Index2': ["i21", "i22", "i23"],
'Index3': ["i31", "i32", "i33"],
'column1': [2, 3,4],
'column2': [5, 8, 5]}
df = pd.DataFrame(data=d)
這應該適合:
a = []
for i in range(df.shape[0]):
a.append({"Index3": df.iloc[2,i],"column 1": df.iloc[i,3], "column2": df.iloc[i,4]})
資源:
[{'Index3': 'i13', 'column 1': 2, 'column2': 5},
{'Index3': 'i23', 'column 1': 3, 'column2': 8},
{'Index3': 'i33', 'column 1': 4, 'column2': 5}]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/373978.html
下一篇:如何向“/users?<some-query-here>”發出GET請求,然后所有滿足查詢條件的用戶都以JSON陣列形式回傳
