簡體   English   中英

如何檢查 dataframe 列是否包含字典的任何值,如果為真,則將字典值復制到 DF 的新列中?

[英]How do I check if a dataframe column contains any values of a dictionary and if true copy the dictionary values in a new column of the DF?

我正在努力完成三件事。 首先,我想檢查dictionary中的任何值是否包含在dataframe列的任何值中。 其次,對於dataframe列中包含dictionary值的每個值,我想在正在檢查的列旁邊的新列中輸入該dictionary值。 第三,我想在新列中輸入dictionary值的關聯鍵。 我想我在確定包含 function 是否為真時陷入了if condition 請注意,這只是一個示例,真正的字典將有數百個鍵/值,並且字典有大約一百萬行。 此外,雖然很少見,但dataframe列可能包含字典中的多個值。 如果有更好的方法來做這一切,我願意接受。

字典 - dict1:

{'Delay one': ['this delay happens', 'this delay may happen'],
 'Delay two': ['this delay happens a lot', 'this delay happens almost'],
 'Other': ['this delay occurs']}

Dataframe - df2:

col1            col2                             col3
0     1   1/1/2021 2:07         this delay happens often
1     2  1/5/2021 19:21    this delay happens a lot here
2     3   1/1/2021 2:51   this delay happens almost alot
3     4   1/1/2021 5:24  this delay happens almost never
4     5   1/1/2021 5:24                              nan
5     9  1/1/2021 10:55                             null

期望的結果:

col1    col2    col3    contain_value   associated_key
0   1   1/1/2021 2:07   this delay happens often.   this delay happens  Delay one
1   2   1/5/2021 19:21  this delay happens a lot here.  this delay happens a lot    Delay two
2   3   1/1/2021 2:51   this delay happens almost alot. this delay happens almost   Delay two
3   4   1/1/2021 5:24   this delay happens almost never.    this delay happens almost   Delay two
4   5   1/1/2021 5:24   NaN NaN NaN
5   9   1/1/2021 10:55  Null    NaN NaN

代碼:

# create dictionary
dict1 = df.groupby('col2')['col3'].agg(list).to_dict()

# Series created from dataframe so that contain function can be used; not sure if entire dataframe # can be used with contained function and if that would be better
series = df2['col3']

# function - if value in series contains any dict1 values put dict1 value in new column

def contain(note):
    for key, value in dict1.items():
        for v in range(len(value)):
            contain = series[(series.str.contains(value[v]))]
            if contain:
                return v
    
# apply function to get dictionary values that are contained in DF column
df2['contain_value'] = df2['col3'].apply(lambda x: contain(x))

# Not sure how to incorporate in the contain function on how to get key
df2['associated_key'] = df2['col3'].apply(lambda x: contain(x))

錯誤:

ValueError                                Traceback (most recent call last)
C:\Users\HECTOR~1.HER\AppData\Local\Temp/ipykernel_25036/3873876505.py in <module>
     25 
     26 # xact_notes_match_comments
---> 27 df2['contain_value'] = df2['col3'].apply(lambda x: contain(x))
     28 
     29 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwargs)
   4355         dtype: float64
   4356         """
-> 4357         return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
   4358 
   4359     def _reduce(

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply(self)
   1041             return self.apply_str()
   1042 
-> 1043         return self.apply_standard()
   1044 
   1045     def agg(self):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
   1096                 # List[Union[Callable[..., Any], str]]]]]"; expected
   1097                 # "Callable[[Any], Any]"
-> 1098                 mapped = lib.map_infer(
   1099                     values,
   1100                     f,  # type: ignore[arg-type]

C:\ProgramData\Anaconda3\lib\site-packages\pandas\_libs\lib.pyx in pandas._libs.lib.map_infer()

C:\Users\HECTOR~1.HER\AppData\Local\Temp/ipykernel_25036/3873876505.py in <lambda>(x)
     25 
     26 # xact_notes_match_comments
---> 27 df2['contain_value'] = df2['col3'].apply(lambda x: contain(x))
     28 
     29 

C:\Users\HECTOR~1.HER\AppData\Local\Temp/ipykernel_25036/3873876505.py in contain(note)
     20         for v in range(len(value)):
     21             contain = series[(series.str.contains(value[v]))]
---> 22             if contain:
     23                 return contain
     24 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
   1535     @final
   1536     def __nonzero__(self):
-> 1537         raise ValueError(
   1538             f"The truth value of a {type(self).__name__} is ambiguous. "
   1539             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

您的contain function 應如下所示:

def contain(note):
    for key, value in dict1.items():
        for v in range(len(value)):
            contain = series[(series.str.contains(value[v]))]
            if not contain.empty:
                return v

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM