'str' 和 'int' pandas 函数的实例之间不支持“>”以获取阈值

'>' not supported between instances of 'str' and 'int' pandas function for getting threshold

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

我的目标是获得一个带有新列 Keep_Words 的 df,如下所示,其中 Keep_Words

中仅保留 85 以上的分数
     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)]

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

由于您只想更改 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:

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:

其中 column_tup 是作为 pd.Series 的整行,但它在布尔上下文中的行为不明确。