我有一个Pandas数据框,其列名为“image\u versions2.candidates”:
df_myposts['image_versions2.candidates']
这给了我:
^{pr2}$我试图将这个url提取到一个新的列中,例如“image_url”。在
我可以使用以下代码提取单个URL:
df_myposts['image_versions2.candidates'][0][0]['url']
'https:/XXX'
但在第二行中,由于de NaN值,它给出了以下错误:
df_myposts['image_versions2.candidates'][1][0]['url']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-64-3f0532195cb7> in <module>
----> 1 df_myposts['image_versions2.candidates'][1][0]['url']
TypeError: 'float' object is not subscriptable
我正在尝试使用某种类型的循环和if条件,但我遇到了类似的错误消息:
for i in df_myposts['image_versions2.candidates']:
if type(i[0]) == 'list':
在不丢失NaN行的情况下,Wich可能是更好的选择? 我有另一列的Id为,所以我想保留关系Id<;->;url。 谢谢
我们可以在这里使用} 来提取
list comprehension
和^{URL
标记:使用@amanb的设置数据帧
{{cd2>可以使用一个元素的列表。然后使用}
^{pr2}$to_dict
和{为了得到
您可以使用
join
添加到df
或者你可以替换柱子
我的实际建议
但我最喜欢的是用
pd.io.json.json_normalize
来代替字典的魔力。在使用:
相关问题 更多 >
编程相关推荐