我對我的熊貓資料框的資料型別感到困惑,不知道如何將我的條目分成幾列。
資料如下:
Name1 Name2
0 [0.1,0.2,0.3] [{'label': 'Neutral', 'score': 0.60}]
1 [0.4,0.5,0.6] [{'label': 'Negative', 'score': 0.60}]
2 [0.7,0.8,0.9] [{'label': 'Positive', 'score': 0.60}]
結果應如下所示:
Name1 N1 N2 N3 Name2 Label Score
0 [0.1,0.2,0.3] 0.1 0.2 0.3 [{'label': 'Neutral','score': 0.60}] Neutral 0.60
1 [0.4,0.5,0.6] 0.4. 0.5. 0.6 [{'label': 'Negative','score': 0.60}] Negative 0.60
2 [0.7,0.8,0.9] 0.7 0.8 0.9 [{'label': 'Positive','score': 0.60}] Positive 0.60
原樣
對python不太有信心,但我需要處理包含 fwe 100k 條目的大型資料集。
非常感謝幫助!
最好的
uj5u.com熱心網友回復:
您可以使用pandas.DataFrame.join和pandas.Series.tolist。
df = df.join(
pd.DataFrame(df['Name1'].tolist(), columns=['N1', 'N2', 'N3']
)).join(pd.DataFrame(df['Name2'].apply(lambda x: x[0]).tolist()))
print(df)
輸出:
Name1 Name2 N1 N2 N3 label score
0 [0.1, 0.2, 0.3] [{'label': 'Neutral', 'score': 0.6}] 0.1 0.2 0.3 Neutral 0.6
1 [0.4, 0.5, 0.6] [{'label': 'Negative', 'score': 0.6}] 0.4 0.5 0.6 Negative 0.6
2 [0.7, 0.8, 0.9] [{'label': 'Positive', 'score': 0.6}] 0.7 0.8 0.9 Positive 0.6
輸入資料框:
df = pd.DataFrame({
'Name1' : [[0.1,0.2,0.3], [0.4,0.5,0.6], [0.7,0.8,0.9]] ,
'Name2' : [
[{'label': 'Neutral', 'score': 0.60}],
[{'label': 'Negative', 'score': 0.60}],
[{'label': 'Positive', 'score': 0.60}]
]
})
uj5u.com熱心網友回復:
您可以在特定列上使用 to_list() 函式來使列脫離串列。
您可以在此鏈接下找到更多內容:
https ://datascienceparichay.com/article/split-pandas-column-of-lists-into-multiple-columns/
要使用 dict 做類似的事情,請參閱此頁面:
https ://stackoverflow.com/questions/38231591/split-explode-a-column-of-dictionaries-into-separate-columns-with-pandas
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/519468.html
標籤:Python熊猫数据框
上一篇:如何回傳不是第一行的行的名稱,該行在r的列中具有最大值?
下一篇:用熊貓資料框中的特殊字符替換字串
