如果像我的实际代码那样在列表中有许多df(具有相同的列)更有效,我想获得df列的不同条目。目的是保留“ ID”列中的所有现有ID。 我的代码可以运行,但是我希望它更有效。
list_of_dicts = [[{'ID' : A,
'timestamp': '2020-05-10T03:44:50+00:00',
'status': 'online'},
{'ID' : A,
'timestamp': '2020-05-10T05:45:50+00:00',
'status': 'online'},
{'ID' : B,
'timestamp': '2020-05-10T12:45:50+00:00',
'status': 'online'},
{'ID' : C,
'timestamp': '2020-05-10T04:45:50+00:00',
'status': 'online'},
{'ID' : A,
'timestamp': '2020-05-10T03:48:50+00:00',
'status': 'offline'}], [{...}{...}...]...]
(不同列表中的字典键始终相同)
我的代码:
stat = []
for i in range(len(list_of_dicts)):
stat_i = pd.DataFrame(list_of_dicts[i])
for x in range(len(stat_i)):
a = stat_i.loc[x, 'ID']
if a in stat:
continue
else:
stat.append(a)
期望输出:
stat = ['A', 'B', 'C']
答案 0 :(得分:0)
您需要遍历列表并创建一个数据框以获取唯一项,也可以不使用熊猫来做到这一点。
使用列表理解。
stat = pd.concat([pd.DataFrame(item) for item in list_of_dicts])['ID'].unique()
array(['A', 'B', 'C'], dtype=object)