python - 在“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)]
如何更改我的代码以达到我的目标?
解决方案
由于您仅尝试更改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
但它在布尔上下文中的行为是模棱两可的。
推荐阅读
- ios - IOS加密在Xcode11上面不同
- macos - 从标记到 Cryptotoken 套件代码签名
- apache - AMPPS for Mac,本地虚拟主机现在不再工作
- riscv - 协处理器累加器示例的 32 位海拉缓存访问
- cypress - 在 Cypress 中只运行失败的测试用例
- sql-server - Siteground SQL 数据库到 Power BI
- php - 当我已经登录时,第二个选项卡重定向到登录页面,在 codeigniter 应用程序中
- java - Android Q 创建和保存文件
- java - com.sun.jersey.spi.inject.Errors.processErrorMessages 处的错误 Java Jersey com.sun.jersey.spi.inject.Errors$ErrorMessagesException(Errors.java:170)
- python - NDVI值计算和图像处理