gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:11:59 28 4
gpt4 key购买 nike

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

  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 forinclude/exclude as well as list-like. (GH16855)

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

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com