Python 从pandas中的字典列表中提取元素
在数据框的一列中,我对数据框的每一行都有以下分数:Python 从pandas中的字典列表中提取元素,python,pandas,list,dictionary,multi-index,Python,Pandas,List,Dictionary,Multi Index,在数据框的一列中,我对数据框的每一行都有以下分数: [{'score': 100, 'bonus': 10}, {'score': 60, 'bonus': 0}] [{'score': 80, 'bonus': 20}, {'score': 90, 'bonus': 30}] 输出 输出将是两个新列,用于每行的分数变化和奖金变化。所以第一行的df.delta_分数为-40,df.delta_奖金为-10,第二行的df.delta_分数为10,df.delta_奖金为10 [{'score':
[{'score': 100, 'bonus': 10}, {'score': 60, 'bonus': 0}]
[{'score': 80, 'bonus': 20}, {'score': 90, 'bonus': 30}]
输出
输出将是两个新列,用于每行的分数变化和奖金变化。所以第一行的df.delta_分数为-40,df.delta_奖金为-10,第二行的df.delta_分数为10,df.delta_奖金为10
[{'score': 100, 'bonus': 10}, {'score': 60, 'bonus': 0}, -40, -10]
[{'score': 100, 'bonus': 10}, {'score': 60, 'bonus': 0}, 10, 10]
我想计算每个人(行)的得分和奖金的值变化,并用结果值生成新列。我对这种数据类型很困惑,因为它似乎是一个字典列表,但由于每个字典都有相同的键,我想使用一个运算符来计算差异。任何帮助都将不胜感激。我们可以进行
分解
然后获得数据帧
s = df['Col'].explode()
calcu = pd.DataFrame(s.tolist(), index=s.index)
calcu
Out[170]:
score bonus
0 100 10
0 60 0
1 80 20
1 90 30
在这之后你可以做什么
calcu.groupby(level=0)...(calculation follow by groupby)
我们只需创建两个新列
Bonus
和Score
。您可以将列表理解与.get()
一起使用,根据键bonus
和score
检索值。然后,通过订阅[1]
并减去[0]
import pandas as pd
df = pd.DataFrame({'dict_col': [[{'score': 100, 'bonus': 10}, {'score': 60, 'bonus': 0}],
[{'score': 80, 'bonus': 20}, {'score': 90, 'bonus': 30}]]})
df['Bonus'] = df['dict_col'].apply(lambda x: [d.get('bonus') for d in x][1] - [d.get('bonus') for d in x][0])
df['Score'] = df['dict_col'].apply(lambda x: [d.get('score') for d in x][1] - [d.get('score') for d in x][0])
df
Out[1]:
dict_col Bonus Score
0 [{'score': 100, 'bonus': 10}, {'score': 60, 'b... -10 -40
1 [{'score': 80, 'bonus': 20}, {'score': 90, 'bo... 10 10
你如何知道奖金的差额应该是
10
还是-10
?你的预期结果是什么,不是用文字,而是如果你能用数字表示你的预期结果?正如David所说,请将预期结果作为文章的一部分。编辑以包括输出