gpt4 book ai didi

python - 从 pandas 的 JSON 列中提取属于特定键的值

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:31 25 4
gpt4 key购买 nike

例如,我有一个如下所示的数据框:

    name      eventlist
0 a [{'t': '1234', 'n': 'user_engagem1'},{'t': '2345', 'n': 'user_engagem2'},{'t': '3456', 'n': 'user_engagem3'}]
1 b [{'t': '2345', 'n': 'user_engagem4'},{'t': '1345', 'n': 'user_engagem5'},{'t': '1356', 'n': 'user_engagem6'},{'t': '1345', 'n': 'user_engagem5'},{'t': '1359', 'n': 'user_engagem6'}]
2 c [{'t': '1334', 'n': 'user_engagem3'},{'t': '2345', 'n': 'user_engagem4'},{'t': '3556', 'n': 'user_engagem2'}]

我用一个字符串尝试了 re.findall,它似乎有效,我得到了这样的结果['1234', '2345', '3456'], 但我不能将它应用到数据框中

#code 1,apply to string successfully
str="[{'t': '1234', 'n': 'user_engagem'},{'t': '2345', 'n': 'user_engagem'},{'t': '3456', 'n': 'user_engagem'}]"
print(re.findall(r"t': '(.+?)', '", str))

#code 2,apply to dateframe doesn't work
df['t']=df['events'].str.findall(r"t': '(.+?)', '", df['events'])
print(list)

我想得到这样的结果

    name      eventlist

0 a ['1234', '2345', '3456']
1 b ['2345', '1345','1234','1356', '1356']
2 c ['1334', '2345', '3556']

或者更好的是,我可以获得这样的结果

    name      t_first       t_last
0 a 1234 3456
1 b 2345 1359
2 c 1334 3556

最佳答案

df['eventlist'] = df['eventlist'].map(lambda x:[i['t'] for i in x])
df
name eventlist
0 a [1234, 2345, 3456]
1 b [2345, 1345, 1356, 1345, 1359]
2 c [1334, 2345, 3556]

df['t_first'] = df['eventlist'][0]
df['t_last']=df['eventlist'].map(lambda x:x[len(x)-1])
df = df[['name','t_first','t_last']]
df
name t_first t_last
0 a 1234 3456
1 b 2345 1359
2 c 3456 3556

关于python - 从 pandas 的 JSON 列中提取属于特定键的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53910073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com