首页 > 解决方案 > 在“str”和“int”pandas 函数的实例之间不支持“>”以获得阈值

问题描述

我有一个df

import pandas as pd
df= pd.DataFrame({'ID': [1,2,3], 
                  
                           'Text':['This num dogs and cats is (111)888-8780 and other',
                              'dont block cow 23 here',
                              'cat two num: dog  and cows here'], 
                                   
                                  'Match':[[('cats', 86), ('dogs', 86), ('dogs', 29)],
                                   [('cow', 33), ('dont', 57), ('cow', 100)], 
                                             [('cat', 100), ('dog', 100), ('cows', 86)] ]
                      })

它看起来像这样

    ID                       Text                                   Match
0   1   This num dogs and cats is (111)888-8780 and other   [(cats, 86), (dogs, 86), (dogs, 29)]
1   2   dont block cow 23 here                              [(cow, 33), (dont, 57), (cow, 100)]
2   3   cat two num: dog and cows here                      [(cat, 100), (dog, 100), (cows, 86)]

我的目标是创建一个函数,只保留Match列中高于某个阈值(例如 80)的某些项目,所以我尝试了以下

def threshold(column):
    
    column_tup = column
    
    keep_tuple = []
    
    for col in column_tup:
        if column_tup > 80:
            keep_tuple.append()
    
    return pd.Series([keep_tuple], index = ['Keep_Words'])

df_thresh = df.join(df.apply(lambda x: threshold(x),  axis = 1))

但这给了我一个错误

'>' not supported between instances of 'str' and 'int'

我的目标是获得一个带有新列的 df,Keep_Words如下所示,其中仅保留高于 85 的分数Keep_Words

     ID    Text    Match   Keep_Words
0   1      data    data   [(cats, 86), (dogs, 86)]              
1   2      data    data   [(cow, 100)]
2   3      data    data   [(cat, 100), (dog, 100)]

如何更改我的代码以达到我的目标?

标签: pythonpandaslistfunctiontuples

解决方案


由于您仅尝试更改Match列,因此您不妨只将该列传递给apply

df.Match.apply(threshold)

我们不再使用axis参数,因为它是一个我们正在应用的系列,而且它只有一个轴。

然后,每次调用您的函数时,df.Match都会传递一行并分配给函数参数,因此我们可以将函数签名重命名为:

def threshold(match_row):

为了可读性。

因此,match_row将是一个列表,例如,在第一轮中,它将是[(cats, 86), (dogs, 86), (dogs, 29)]. 我们可以像您一样进行迭代,但使用 2 个 for 循环变量:

for name, val in match_row:

所以这name将成为每个元组的第一个条目,并且val是第二个。现在我们可以进行过滤:

keep_tuple = []
for name, val in match_row:
    if val > 85:
        keep_tuple.append((name, val))

这很好,但不是 Pythonic,因为有列表推导:

keep_tuple = [(name, val) for name, val in match_row if val > 85]

最后我们可以像你一样返回这个:

return pd.Series([keep_tuple], index=["Keep_Words"])

至于调用和分配,我们可以join像您一样:

df_thresh = df.join(df.Match.apply(threshold))

总而言之,

def threshold(match_row):
    keep_tuple = [(name, val) for name, val in match_row if val > 85]
    return pd.Series([keep_tuple], index=["Keep_Words"])

df_thresh = df.join(df.Match.apply(threshold))

这使

>>> df_thresh

   ID                                               Text                                 Match                            Keep_Words
0   1  This num dogs and cats is (111)888-8780 and other  [(cats, 86), (dogs, 86), (dogs, 29)]              [(cats, 86), (dogs, 86)]
1   2                             dont block cow 23 here   [(cow, 33), (dont, 57), (cow, 100)]                          [(cow, 100)]
2   3                    cat two num: dog  and cows here  [(cat, 100), (dog, 100), (cows, 86)]  [(cat, 100), (dog, 100), (cows, 86)]

最后,对于你得到的错误:我没有得到那个错误,但是臭名昭著

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

错误,这是因为这条线

if column_tup > 80:

wherecolumn_tup是一整行,pd.Series但它在布尔上下文中的行为是模棱两可的。


推荐阅读