如何获得看起来像字典的字符串列并获取它的最后一项? [英] How can I get a string column that look like a dictionary and get the last item of it?

查看:33
本文介绍了如何获得看起来像字典的字符串列并获取它的最后一项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框:

I have a dataframe that looks like this:

col_A      col_B
AA         {"0":10,"5":13,"8":20}
BB         {"0":2,"3":34,"5":40,"15":100}
CC         {"2":5,"5":19,"15":200,"20":200,"30":340}

如您所见,col_B是某种类型的字典.我需要输入最后一个数字.基本上,我需要实现以下目标:

As you can see, col_B is some sort of dict. I need to get the last number in it. Basically, I need to achieve this:

col_A      col_B                                        col_C
AA         {"0":10,"5":13,"8":20}                       20
BB         {"0":2,"3":34,"5":40,"15":100}               100
CC         {"2":5,"5":19,"15":200,"20":200,"30":340}    340

由于col_B没有固定的长度,因此我试图使用str.count(',')来计数项目的数量,并使用str.split(':')来分割数量.但这似乎太复杂了.我想知道这里是否缺少一种更简单的方法.

Since col_B doesn't have a fixed length, I was trying to use str.count(',') to count the number of items and them str.split(':') to split the numbers. But this seems like too complex. I was wondering if there is an easier approach that I'm missing in here.

推荐答案

尝试使用 str.extract :

# all digits between `:` and `}`
# also, for any characters, not necessarily digits
# ':([^:]+)}'
df['col_C'] = df['col_B'].str.extract(':(\d+)}')

输出:

  col_A                                      col_B col_C
0    AA                     {"0":10,"5":13,"8":20}    20
1    BB             {"0":2,"3":34,"5":40,"15":100}   100
2    CC  {"2":5,"5":19,"15":200,"20":200,"30":340}   340

这篇关于如何获得看起来像字典的字符串列并获取它的最后一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆