代码之家  ›  专栏  ›  技术社区  ›  codproe

Python创建分类变量错误

  •  -3
  • codproe  · 技术社区  · 3 年前

    我需要为RAM类别创建分类变量。

    基本:RAM[0-4]

    中间:RAM[5-8]

    高级:RAM[8-12]

    命令:

    df['Memory']=pd.cut(df['RAM '], [0,4,8,12], include_lowest=True, labels=['Basic','Intermediate', 'Advaced'])
    

    错误:

    TypeError                                 Traceback (most recent call last)
    <ipython-input-58-5c93d7c00ba2> in <cell line: 1>()
    ----> 1 df['Memory']=pd.cut(df['RAM '], [0,4,8,12], include_lowest=True, labels=['Basic', 'Intermediate', 'Advaced'])
    
    1 frames
    /usr/local/lib/python3.9/dist-packages/pandas/core/reshape/tile.py in _bins_to_cuts(x, bins, right, labels, precision, include_lowest, dtype, duplicates, ordered)
        425 
        426     side: Literal["left", "right"] = "left" if right else "right"
    --> 427     ids = ensure_platform_int(bins.searchsorted(x, side=side))
        428 
        429     if include_lowest:
    
    TypeError: '<' not supported between instances of 'int' and 'str'
    

    你能帮我修一下吗?我是Python的新手。

    1 回复  |  直到 3 年前
        1
  •  1
  •   Timeless    3 年前

    你的列中似乎有类似数字的值 RAM ,所以使用 to_numeric :

    df['Memory']= pd.cut(pd.to_numeric(df['RAM '], errors="coerce"), bins=[0,4,8,12],
                         include_lowest=True, labels=['Basic', 'Intermediate', 'Advaced'])
    

    带有 实例 :

    df = pd.DataFrame({"RAM": np.random.randint(low=1, high=12, size=100).astype(str)})
    
    df["RAM"] = ​pd.to_numeric(df["RAM"], errors="coerce")
    ​
    df["Memory"] = pd.cut(df["RAM"], bins=[0, 4, 8, 12],
                          labels=["Basic", "Intermediate", "Advaced"])
    

    输出:

       RAM        Memory
    0    2         Basic
    1    2         Basic
    2    6  Intermediate
    ..  ..           ...
    97   6  Intermediate
    98   1         Basic
    99   7  Intermediate
    
    [100 rows x 2 columns]