从Pandas系列中提取“url”值

2024-09-30 04:35:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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。 谢谢


Tags: 数据inimageidurlpandasdfif
3条回答

我们可以在这里使用list comprehension^{}来提取URL标记:

df.fillna('None', inplace=True)

df['image_url'] = [
    d['image_versions2.candidates']['url'] if d['image_versions2.candidates'] != 'None' else 'None' for idx, d in df.iterrows()
]

print(df)
                          image_versions2.candidates   image_url
0  {'width': 750, 'height': 498, 'url': 'https:/X...  https:/XXX
1                                               None        None
2  {'width': 750, 'height': 498, 'url': 'https:/Y...  https:/YYY
3  {'width': 750, 'height': 498, 'url': 'https:/Z...  https:/ZZZ

使用@amanb的设置数据帧

df = pd.DataFrame({
    'a':[1,2,3],
    'b':[
        [{'width': 750, 'height': 498, 'url': 'https:/XXX'}],
        [{'width': 750, 'height': 498, 'url': 'https:/YYY'}],
        None
    ]
})

{{cd2>可以使用一个元素的列表。然后使用to_dict和{}

^{pr2}$

为了得到

   width  height         url
0    750     498  https:/XXX
1    750     498  https:/YYY

您可以使用join添加到df

df.join(pd.DataFrame.from_dict(df.b.dropna().str[0].to_dict(), orient='index'))

   a                                                  b  width  height         url
0  1  [{'width': 750, 'height': 498, 'url': 'https:/...  750.0   498.0  https:/XXX
1  2  [{'width': 750, 'height': 498, 'url': 'https:/...  750.0   498.0  https:/YYY
2  3                                               None    NaN     NaN         NaN

或者你可以替换柱子

df.assign(b=pd.DataFrame.from_dict(df.b.dropna().str[0].to_dict(), orient='index').url)

   a           b
0  1  https:/XXX
1  2  https:/YYY
2  3         NaN

我的实际建议

但我最喜欢的是用pd.io.json.json_normalize来代替字典的魔力。在

df.assign(b=pd.io.json.json_normalize(df.b.dropna().str[0]).url)

   a           b
0  1  https:/XXX
1  2  https:/YYY
2  3         NaN

使用:

df = pd.DataFrame({'a':[1,2,3], 'b':[[{'width': 750, 'height': 498, 'url': 'https:/XXX'}], [{'width': 750, 'height': 498, 'url': 'https:/YYY'}], None]})
# df.dropna(inplace = True) #drop rows with null values
# to preserve rows with NaN, first replace NaN values with a scalar/dict value
df.fillna('null', inplace=True)
df['c'] = df['b'].apply(lambda x: [y['url'] if isinstance(x, list) else 'null' for y in x])
df['c'] = df['c'].apply(lambda x:x[0]) #get only the url from the list

#Output:
    a                        b                                   c
0   1   [{'width': 750, 'height': 498, 'url': 'https:/...   https:/XXX
1   2   [{'width': 750, 'height': 498, 'url': 'https:/...   https:/YYY
2   3                       null                                null

相关问题 更多 >

    热门问题