我有一個看起來像這樣的列表:[[('category', 'evaluation'), ('polarity', 'pos'), ('strength', '1'), ('type', 'good')],[('category', 'intensifier'), ('type', 'shifter')],[('category', 'evaluation'), ('polarity', 'pos'), ('strength', '2'), ('type', 'good')],請注意,并非所有列表都包含所有屬性。如果可能的話,我想將其轉換為 DataFrame,其中每個列表代表一個新行,列的名稱將由第一個元素給出(例如'category'、'polarity'、'strength'、'type ')。最后,DataFrame 應該如下所示: category polarity strength typedf[0]: evaluation pos 1 gooddf[1]: intensifier NaN NaN shifterdf[2]: evaluation pos 2 good任何幫助將不勝感激。
1 回答

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
您可以將每個列表轉換為字典:
import pandas as pd
data = [[('category', 'evaluation'), ('polarity', 'pos'), ('strength', '1'), ('type', 'good')],
[('category', 'intensifier'), ('type', 'shifter')],
[('category', 'evaluation'), ('polarity', 'pos'), ('strength', '2'), ('type', 'good')]]
df = pd.DataFrame(data=[dict(e) for e in data])
print(df)
輸出
category polarity strength type
0 evaluation pos 1 good
1 intensifier NaN NaN shifter
2 evaluation pos 2 good
添加回答
舉報
0/150
提交
取消