Pandas 数据帧从 json 中提取值,作为请求的内容(JSON)返回,

Pandas dataframe extracting value from json, which returned from as content(JSON) from request,

Pandas 数据帧从 JSON 中提取值,作为请求的内容返回。

import pandas as pd
import pandas as pd
import json 
import requests
import ast
from pandas.io.json import json_normalize

df['response'] = df.URL.apply(lambda u: requests.get(u).content)

df.head()

b'{"error":0,"short":"http:\/\/192.168.42.72\/ECyKY"}'
b'{"error":0,"short":"http:\/\/192.168.42.72\/IsMgE"}'

当我们使用Python而不使用Pandas时,我们可以只使用:

resp = requests.get(u)
y=resp.json()
print(y)
print(y['short'])

将短值存储为“http://192.168.42.72/ECyKY” 花几个小时试图让它与 Pandas 一起工作但没有运气,有什么提示吗?

不使用response.get.content直接使用response.get.json然后使用Series.str.get从字典中提取键short对应的值然后赋给新列short:

df['response'] = df['URL'].apply(lambda u: requests.get(u).json())
df['short'] = df['response'].str.get('short')

# print(df)
                                            response                       short
0  {'error': 0, 'short': 'http://192.168.42.72/EC...  http://192.168.42.72/ECyKY
1  {'error': 0, 'short': 'http://192.168.42.72/Is...  http://192.168.42.72/IsMgE