I have a list of lists that looks like this:
[[('category', 'evaluation'), ('polarity', 'pos'), ('strength', '1'), ('type', 'good')],
[('category', 'intensifier'), ('type', 'shifter')],
[('category', 'evaluation'), ('polarity', 'pos'), ('strength', '2'), ('type', 'good')],
Note that not all lists contain all the attributes.
I would like, if possible, to convert this to a DataFrame, where each list represents a new row and the names of the columns would be given by the first element (eg 'category' , 'polarity' , 'strength' , 'type' ). In the end, the DataFrame should look like this:
category polarity strength type
df[0]: evaluation pos 1 good
df[1]: intensifier NaN NaN shifter
df[2]: evaluation pos 2 good
Any help would be greatly appreciated.
You could transform each list into a dictionary:
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)
Output
category polarity strength type
0 evaluation pos 1 good
1 intensifier NaN NaN shifter
2 evaluation pos 2 good
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.