如何使函数根据inpu中包含的字进行过滤

2024-06-01 04:44:33 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我想这样做,如果输入x包含Averagefirearmsper100,那么应该给出以下输出

def correlation(x):
    if x.str.contains("Averagefirearmsper100"):
        print(corr1)
        corr1plot.show()
        if corr1 > -0.99 & corr1 < -0.4 :
            print("strong negative relation")
        elif corr1 > -0.39 & corr1 < -0.20 :
            print('weak negative relation')
        elif corr1 > -0.19 & corr1 < 0.19 :
            print('no or negligible relation')
        elif corr1 > 0.20 & corr1 <0.39 :
            print('weak positive relation')
        elif corr1 > 0.4 & corr1 < 0.99 :
            print('strong positive relation')

我得到以下错误

AttributeError                            
Traceback (most recent call last)
<ipython-input-57-dc3f5f5c9df1> in <module>()
----> 1 correlation("HDI")

<ipython-input-56-308ea6ff6259> in correlation(x)
      1 def correlation(x):
----> 2     if x.str.contains("Averagefirearmsper100"):
      3         print(corr1)
      4         corr1plot.show()
      5         if corr1 > -0.99 & corr1 < -0.4 :

AttributeError: 'str' object has no attribute 'str'

Tags: ifdefshowstrongprintelifcontainsrelation
1条回答
网友
1楼 · 发布于 2024-06-01 04:44:33

如果输入x的类型为str,则会发生错误,因为str没有名为str的属性

您可以尝试从更改代码

if x.str.contains("Averagefirearmsper100"):

if "Averagefirearmsper100" in x:

相关问题 更多 >