简体   繁体   English

如何定义 function 以从嵌套字典中提取 python 中每一行的值

[英]How do I define a function to extract values from nested dictionary for each row in python

I have a column named 'urls' in dataframe 'df' that each row consists of nested dictionaries with a URL and whether it is malicious or not.我在 dataframe 'df' 中有一个名为 'urls' 的列,每一行都包含嵌套字典和 URL 以及它是否是恶意的。 I'd like to extract only the value of the nested dictionary for each row.我只想为每一行提取嵌套字典的值。

0    {'url example 1': {'malicious': False}}
1    {'url example 2': {'malicious': False}}  

By defining a function, I'd like to use 'apply' function to get the result for each row.通过定义 function,我想使用“应用”function 来获得每一行的结果。

Here's the sample function that I have defined.这是我定义的示例 function。

def urlconcern(url):
    try:
        r = s.lookup_urls([url]) 
        return r.values()
    except:
        pass

After running this with 'apply' function使用“应用”运行此 function

df['urls'].apply(urlconcern)

This only gives the result below with round bracket (strangely)这只给出了下面带有圆括号的结果(奇怪)

0    ({'malicious': False})
1    ({'malicious': False})

The desired answer would be期望的答案是

False
False

Could there be any way to do so?有什么办法可以做到吗?

Given pandas series s (I'm assuming it's a pandas series)给定 pandas 系列s我假设它是 pandas 系列)

s = pd.Series([{'url example 1': {'malicious': False}},
               {'url example 2': {'malicious': False}}])

you can use generator expression inside next to look for values of nested dicts.您可以在next中使用生成器表达式来查找嵌套字典的值。

out = s.apply(lambda url: next((v for d in url.values() for k,v in d.items()), None))

Output: Output:

0    False
1    False
dtype: bool

However, I'm not convinced this is what you're looking for since you're losing the url info here.但是,我不相信这是您正在寻找的东西,因为您在这里丢失了 url 信息。

Is this a pandas dataframe?这是 pandas dataframe 吗? Did you instantiate it?你实例化了吗? You may want to look at how this dictionary is constructed because it should be more like您可能想看看这本字典是如何构造的,因为它应该更像

>>> df = {'url':['url example 1', 'url example 2', 'url example 3'], 'malicious': [False, False, True]}
>>> df = pd.DataFrame(df)
>>> df
             url  malicious
0  url example 1      False
1  url example 2      False
2  url example 3       True

Then do然后做

>>> df[df['malicious'] == False]
             url  malicious
0  url example 1      False
1  url example 2      False

I know this doesn't answer your question exactly, but it's a standard way of working with DataFrames and should help your workflow later down the line.我知道这并不能准确回答您的问题,但它是使用 DataFrames 的标准方式,应该有助于您以后的工作流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM