我有一个这样的列表:
[{'FirstOfficer': '1'}, {'SecondOfficer': '2'}, {'ThirdOfficer': '3'},{'FirstOfficer': '4'}, {'SecondOfficer': '5'}, {'ThirdOfficer': '6'},{'FirstOfficer': '7'}, {'SecondOfficer': '8'}, {'ThirdOfficer': '9'},{'FirstOfficer': '10'}, {'SecondOfficer': '11'}, {'ThirdOfficer': '12'}]
我想将其转换为数据框,但是我得到了这样的数据框:
FirstOfficer SecondOfficer ThirdOfficer
0 1 NaN NaN
1 NaN 2 NaN
2 NaN NaN 3
3 4 NaN NaN
4 NaN 5 NaN
5 NaN NaN 6
6 7 NaN NaN
7 NaN 8 NaN
8 NaN NaN 9
9 10 NaN NaN
10 NaN 11 NaN
11 NaN NaN 12
列名可以是任何东西,因此我无法对其进行硬编码。
期望的数据帧是:
FirstOfficer SecondOfficer ThirdOfficer
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12
有人可以建议我解决这个问题吗?
任何帮助将不胜感激。
答案 0 :(得分:2)
使用defaultdict
来存储值以按字典键列出:
from collections import defaultdict
d = defaultdict(list)
for x in L:
a, b = tuple(x.items())[0]
d[a].append(b)
print (d)
df = pd.DataFrame(d)
print (df)
FirstOfficer SecondOfficer ThirdOfficer
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12
答案 1 :(得分:1)
如果性能不是问题,则可以使用:
df=pd.DataFrame(l).apply(lambda x: pd.Series(x.dropna().values))
print(df)
FirstOfficer SecondOfficer ThirdOfficer
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12
答案 2 :(得分:0)
d = [{'FirstOfficer': '1'}, {'SecondOfficer': '2'}, {'ThirdOfficer': '3'}, {'FirstOfficer': '4'}, {'SecondOfficer': '5'}, {'ThirdOfficer': '6'}, {'FirstOfficer': '7'}, {'SecondOfficer': '8'}, {'ThirdOfficer': '9'}, {'FirstOfficer': '10'}, {'SecondOfficer': '11'}, {'ThirdOfficer': '12'}]
keys = list(set([str(i.keys()).split("'")[1] for i in d]))
final_dict = dict()
for key in keys:
final_dict['key'] = [i[key] for i in d if key in i.keys()]
df = pd.DataFrame.from_dict(final_dict)
输出:
FirstOfficer SecondOfficer ThirdOfficer
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12
答案 3 :(得分:0)
一种方法是预处理您的列表
例如:
import pandas as pd
lst = [{'FirstOfficer': '1'}, {'SecondOfficer': '2'}, {'ThirdOfficer': '3'},{'FirstOfficer': '4'}, {'SecondOfficer': '5'}, {'ThirdOfficer': '6'},{'FirstOfficer': '7'}, {'SecondOfficer': '8'}, {'ThirdOfficer': '9'},{'FirstOfficer': '10'}, {'SecondOfficer': '11'}, {'ThirdOfficer': '12'}]
data = []
for i in range(0, len(lst), 3):
temp = []
for d in lst[i:i+3]:
for _, v in d.items():
temp.append(v)
data.append(temp)
df = pd.DataFrame(data, columns=["FirstOfficer", "SecondOfficer", "ThirdOfficer"])
print(df)
输出:
FirstOfficer SecondOfficer ThirdOfficer
0 1 2 3
1 4 5 6
2 7 8 9
3 10 11 12