如何获取Python中每一行都有字典的列的信息 [英] How to get the information of a column in Python which has a dictionary in every row
本文介绍了如何获取Python中每一行都有字典的列的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个从API获得的数据框。我的结果数据框在各列中都有一个字典,我想获取有关它们的信息。
这是我的数据帧的示例:
I have a dataframe obtained from an API. My result dataframe has a dictionary in the columns and I would like to obtain the information on them. This is a sample of my dataframe:
如何获取黄色栏中的值?以及如何将此数据帧保存为CSV?
How to get the values in the yellow columns? and how to save this dataframe in CSV?
在此先感谢您的帮助!
这是从API获取数据帧的代码:
Thanks in advance for your help! This is the code to obtain the dataframe from an API:
r = rq.get('https://api.tfl.gov.uk/Road/A2%2C%20A406%2C%20A1%2C%20A12%2C%20A13/Disruption?app_id=XXXXXXXXXX&app_key=XXXXXXXXX')
r = r.text
df7 = pd.read_json(r)
df7
推荐答案
columns = ['geography','geometry']
for col in columns:
d = df7.loc[0,col]
for key in d.keys():
df7[key+'_'+col] = df7[col].apply(lambda x: x[key] if x is not np.nan else np.nan)
通过用col名称替换地理位置,为所有列尝试此操作
try this for all the columns by replacing geography with the col name
这篇关于如何获取Python中每一行都有字典的列的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文