我有一个清单,如下所示:
[[('category', 'evaluation'), ('polarity', 'pos'), ('strength', '1'), ('type', 'good')],
[('category', 'intensifier'), ('type', 'shifter')],
[('category', 'evaluation'), ('polarity', 'pos'), ('strength', '2'), ('type', 'good')],注意,并非所有列表都包含所有属性。
如果可能的话,我希望将其转换为一个DataFrame,其中每个列表代表一个新行,列的名称将由第一个元素(例如“类别”、“极性”、“强度”、“类型”)指定。最后,DataFrame应该是这样的:
category polarity strength type
df[0]: evaluation pos 1 good
df[1]: intensifier NaN NaN shifter
df[2]: evaluation pos 2 good任何帮助都将不胜感激。
发布于 2019-10-18 12:30:01
您可以将每个列表转换为字典:
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 goodhttps://stackoverflow.com/questions/58450965
复制相似问题