【问题标题】:Pandas error: isin() got an unexpected keyword argument 'case'Pandas 错误:isin() 得到了一个意外的关键字参数“case”
【发布时间】:2019-06-25 19:36:20
【问题描述】:

我有以下代码,其中包括一个大小写不同的列表。我想将 Pandas 代码设置为忽略大小写,使用 case = False,但是我的代码触发了以下错误:

TypeError: isin() 得到了一个意外的关键字参数“case”

import pandas as pd

simple_upstream_types = ("Single rate",
                             "Single rate 2",
                            )


raw_df_simple = raw_df.loc[raw_df['upstream_rate_type'].isin([simple_upstream_types], case=False)]

请帮忙

【问题讨论】:

  • isin 根本不接受 case 参数...
  • 检查isin()的文档

标签: python pandas


【解决方案1】:

您可以使用接受标志的str.contains,并将simple_upstream_types 转换为正则表达式:

import re

raw_df_simple = raw_df[raw_df['upstream_rate_type'].str.contains('|'.join(simple_upstream_types), flags=re.IGNORECASE)]

【讨论】:

    【解决方案2】:

    我最终将 pandas 列格式化为小写条目,这对我的小写列表很有效。

    【讨论】:

      【解决方案3】:

      这是另一种方式。使用numpywhere,创建一个比较类型列表和列值(小写)的新列。然后是一个新的列来说明它是matches 还是unmatched。请看下面的模型:

      import pandas 
      import numpy as np
      df = pd.DataFrame({'UpstreamTypes': ["single rate","Single ratE","Single Rate","Single rate 2"]})
      simple_upstream_types = ["Single rate","Single rate 2"]
      df['Status'] = np.where(df.UpstreamTypes.str.lower().isin([v.lower() for v in list_of_values]),'Matched', 'Unmatched')
      df
      

      结果如下:

      UpstreamTypes   Status
      0   single rate Matched
      1   Single ratE Matched
      2   Single Rate Matched
      3   Single rate 2   Matched
      

      【讨论】:

        猜你喜欢
        • 2018-10-24
        • 1970-01-01
        • 1970-01-01
        • 2021-04-10
        • 2021-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-14
        相关资源
        最近更新 更多