如何从DataFrame列表格式的JSON列中提取price字段值?
问题描述
给定如下Pandas DataFrame:
import pandas as pd data =[['a056cf7d3aeadfc7c4d5d6e37f424554', 11089528586286, [{'rate': 0.0625, 'price': '0.52', 'title': 'IL STATE TAX', 'price_set': {'shop_money': {'amount': '0.52', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.52', 'currency_code': 'USD'}}, 'channel_liable': False}, {'rate': 0.0, 'price': '0.00', 'title': 'IL COUNTY TAX', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'USD'}}, 'channel_liable': False}, {'rate': 0.0, 'price': '0.00', 'title': 'IL CITY TAX', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'USD'}}, 'channel_liable': False}, {'rate': 0.0, 'price': '0.00', 'title': 'IL SPECIAL TAX', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'USD'}}, 'channel_liable': False}]], ['a056cf7d3aeadfc7c4d5d6e37f424554', 11089528651822, [{'rate': 0.0625, 'price': '0.75', 'title': 'IL STATE TAX', 'price_set': {'shop_money': {'amount': '0.75', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.75', 'currency_code': 'USD'}}, 'channel_liable': False}, {'rate': 0.0, 'price': '0.00', 'title': 'IL COUNTY TAX', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'USD'}}, 'channel_liable': False}, {'rate': 0.0, 'price': '0.00', 'title': 'IL CITY TAX', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'USD'}}, 'channel_liable': False}, {'rate': 0.0, 'price': '0.00', 'title': 'IL SPECIAL TAX', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'USD'}}, 'channel_liable': False}]], ['a056cf7d3aeadfc7c4d5d6e37f424554', 11089528717358, [{'rate': 0.0625, 'price': '0.48', 'title': 'IL STATE TAX', 'price_set': {'shop_money': {'amount': '0.48', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.48', 'currency_code': 'USD'}}, 'channel_liable': False}, {'rate': 0.0, 'price': '0.00', 'title': 'IL COUNTY TAX', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'USD'}}, 'channel_liable': False}, {'rate': 0.0, 'price': '0.00', 'title': 'IL CITY TAX', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'USD'}}, 'channel_liable': False}, {'rate': 0.0, 'price': '0.00', 'title': 'IL SPECIAL TAX', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'USD'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'USD'}}, 'channel_liable': False}]], ['a8911732ef329fccffd45fd4ff945b36', 11805558440126, [{'rate': 0.2, 'price': '35.42', 'title': 'AT VAT', 'price_set': {'shop_money': {'amount': '35.42', 'currency_code': 'EUR'}, 'presentment_money': {'amount': '35.42', 'currency_code': 'EUR'}}, 'channel_liable': False}]], ['5506eecfc2ffa7f2b1832e1ccab121fa', 12019803881631, [{'rate': 0.2, 'price': '0.00', 'title': 'GB VAT', 'price_set': {'shop_money': {'amount': '0.00', 'currency_code': 'GBP'}, 'presentment_money': {'amount': '0.00', 'currency_code': 'GBP'}}, 'channel_liable': False}]]] headers = [['token','line','data']] df = pd.DataFrame(data, columns = headers)
其中data列存储的是包含字典对象的列表,需要为每个token和line提取该列中的price值。
遇到的问题
- 使用
explode方法执行以下代码:
s = df.explode('data', ignore_index=True) dfn=s.join(pd.DataFrame([*s.pop('data')], index=s.index))
报错:TypeError: explode() missing 1 required positional argument: 'column'
尝试
json.loads方法失败,因为该列是列表而非字符串。参考相关示例时出现报错:
TypeError: the JSON object must be str, bytes or bytearray, not Series
解决方法
步骤1:修正列名问题
原代码中headers是嵌套列表,导致DataFrame的列名是多层索引(MultiIndex),这是explode报错的核心原因。先将列名改为普通字符串:
df.columns = ['token', 'line', 'data']
步骤2:展开列表并提取字段
使用explode展开data列中的列表,再将字典列转换为DataFrame并合并:
# 展开data列的列表 exploded_df = df.explode('data', ignore_index=True) # 将data列的字典转换为DataFrame tax_details = pd.DataFrame(exploded_df['data'].tolist(), index=exploded_df.index) # 合并原字段和提取的tax信息 result = exploded_df[['token', 'line']].join(tax_details[['price', 'title', 'rate']])
步骤3:按需处理数据(可选)
如果只需要token、line和price,可以直接筛选:
result = result[['token', 'line', 'price']]
最终结果示例
| token | line | price |
|---|---|---|
| a056cf7d3aeadfc7c4d5d6e37f424554 | 11089528586286 | 0.52 |
| a056cf7d3aeadfc7c4d5d6e37f424554 | 11089528586286 | 0.00 |
| a056cf7d3aeadfc7c4d5d6e37f424554 | 11089528586286 | 0.00 |
| ... | ... | ... |
内容的提问来源于stack exchange,提问作者Galat
相关产品推荐
相关产品推荐

