为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Pandas 将字符串与 JSON 进行比较

使用 Pandas 将字符串与 JSON 进行比较

翻过高山走不出你 2021-08-05 15:43:53
我正在使用 Pandas 的 DataFrame。我有这样的情况下,合并2个文件后: fr.item = "ipod"; fr.bucket = {'ipad':34,'ipod':36,'iwatch':27} 注:数据类型在Series这里有没有办法可以在这里检查存储桶 ( ipod) 中的项目并获取值 ( 36)?此外,在没有循环的情况下执行此操作是值得赞赏的,因为我正在做一列到列的比较。输入item    bucketipod    {'ipad':34,'ipod':36,'iwatch':27}ipad    {'ipad':87,'ipod':31,'iwatch':62}输出3687
查看完整描述

3 回答

?
红糖糍粑

TA贡献1815条经验 获得超6个赞

同意@bazingaa,只需使用:

fr.bucket[fr.item]

因此,获取带有命名的字典键,ipod然后像上面一样获取它的值,就是这样。


查看完整回答
反对 回复 2021-08-05
?
qq_遁去的一_1

TA贡献1725条经验 获得超7个赞

item=['ipod','ipad']

bucket=[{'ipad':34,'ipod':36,'iwatch':27},{'ipad':87,'ipod':31,'iwatch':62}]


result_output=[]    # Defining for output

counter=0           # For initialising the counter through the bucket's elements

for key in item:    # For each row value in column1. Column1 is item.

   temp_bucket=dict(bucket[counter]) # For each column's value,it is as a dictionary

   if key in temp_bucket:   # Checking if the column1's value is present in the dictionary of that row (column2)   

      length_1=len(bucket)-1                 # Need to subtract 1 as python is based on 0 indexing     

      result_output.append(temp_bucket[key]) # Appending each row output to the result list.

      if counter<length_1:                   # Need to check if there is sufficient element is present in column2. Otherwise throw error for index.

         counter=counter+1                  # for checking the next element of column2

print(result_output) # 输出列表


查看完整回答
反对 回复 2021-08-05
?
米琪卡哇伊

TA贡献1998条经验 获得超6个赞

我试过了,它奏效了


对于 df.index 中的 i:


#For Bucket

val_bucket=ast.literal_eval(bucket[i])

for key, val in val_bucket.items():

    if my_items[i]== key:

        print ("Value",val)

         break

因此,我已将那些 JSON 值(串联)转换为 dict 并提取值


查看完整回答
反对 回复 2021-08-05
  • 3 回答
  • 0 关注
  • 233 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号