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

我正在使用 Pandas 的 DataFrame。我有这样的情况下,合并2个文件后: fr.item = "ipod"; fr.bucket = {'ipad':34,'ipod':36,'iwatch':27} 注:数据类型在Series这里


有没有办法可以在这里检查存储桶 ( ipod) 中的项目并获取值 ( 36)?此外,在没有循环的情况下执行此操作是值得赞赏的,因为我正在做一列到列的比较。


输入


item    bucket

ipod    {'ipad':34,'ipod':36,'iwatch':27}

ipad    {'ipad':87,'ipod':31,'iwatch':62}

输出


36

87


翻过高山走不出你
浏览 290回答 3
3回答

红糖糍粑

同意@bazingaa,只需使用:fr.bucket[fr.item]因此,获取带有命名的字典键,ipod然后像上面一样获取它的值,就是这样。

qq_遁去的一_1

item=['ipod','ipad']bucket=[{'ipad':34,'ipod':36,'iwatch':27},{'ipad':87,'ipod':31,'iwatch':62}]result_output=[]&nbsp; &nbsp; # Defining for outputcounter=0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# For initialising the counter through the bucket's elementsfor key in item:&nbsp; &nbsp; # For each row value in column1. Column1 is item.&nbsp; &nbsp;temp_bucket=dict(bucket[counter]) # For each column's value,it is as a dictionary&nbsp; &nbsp;if key in temp_bucket:&nbsp; &nbsp;# Checking if the column1's value is present in the dictionary of that row (column2)&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; length_1=len(bucket)-1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Need to subtract 1 as python is based on 0 indexing&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; result_output.append(temp_bucket[key]) # Appending each row output to the result list.&nbsp; &nbsp; &nbsp; if counter<length_1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# Need to check if there is sufficient element is present in column2. Otherwise throw error for index.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;counter=counter+1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # for checking the next element of column2print(result_output) # 输出列表

米琪卡哇伊

我试过了,它奏效了对于 df.index 中的 i:#For Bucketval_bucket=ast.literal_eval(bucket[i])for key, val in val_bucket.items():&nbsp; &nbsp; if my_items[i]== key:&nbsp; &nbsp; &nbsp; &nbsp; print ("Value",val)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break因此,我已将那些 JSON 值(串联)转换为 dict 并提取值
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python