python - 类型错误 : include and exclude must both be non-string sequences

标签 python python-3.x pandas

我有各种数据类型的数据:

  1. 餐厅 ID int64
  2. 餐厅名称对象
  3. 国家/地区代码 int64

...

  • 对文本对象进行评分
  • 投票 int64
  • 数据类型:对象
  • 当我运行下面的代码时:

    too_verbose_columns = (data.select_dtypes(include = 'O')\
        .columns[data.select_dtypes(include='O').nunique() > 2]\  
        .tolist())
    

    我收到此错误:

    TypeError: include and exclude must both be non-string sequences.

    如何解决此错误?

    最佳答案

    Pandas v0.21 之前的版本

    字符串'O'不被视为序列。例如,您需要输入一个列表。此外,无需重复调用select_dtypes:

    data = pd.DataFrame([['1', '2', '3'], ['2', '2', '4'], ['5', '6', '7']],
                        columns=['col1', 'col2', 'col3'])
    
    df = data.select_dtypes(include=['O'])
    
    too_verbose_columns = df.columns[df.apply(pd.Series.nunique) > 2].tolist()
    
    print(too_verbose_columns)
    
    ['col1', 'col3']
    

    col2 超出范围,因为它只有 2 个唯一元素。

    Pandas v0.21+

    从 Pandas v0.21 开始,select_dtypes now accepts a scalar :

    DataFrame.select_dtypes() now accepts scalar values for include/exclude as well as list-like. (GH16855)

    关于python - 类型错误 : include and exclude must both be non-string sequences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250600/

    相关文章:

    Python:迭代一个集合,这样我们就不会多次比较相同的对象?

    Python - 读取 Powershell 脚本输出,使用子进程,希望逐行接收标准输出

    python - 如何在 Pandas 中 append 数据帧而无需交错格式

    python - 输出列表中负数的索引(使用结构递归)

    python - 如何避免将重复项插入数据库的最有效方法?

    python - file.replace ('abcd' ) 也替换了 'abcde' 如何只替换精确值?

    django - 在测试中使用 Django 缓存强制连接错误

    python - 通过python程序访问网络文件夹

    python - Pandas 滚动窗口 - datetime64[ns] 未实现

    python - 生成特定月份的日期?