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

在python中如何将数据帧列转换为int

  •  0
  • user1471980  · 技术社区  · 7 年前

    我试图在python中将数据类型转换为inter

    当我这样做的时候

    df1.dtypes
    

    max          object
    

    我需要将df1['max']转换为int

    当我这样做时:

    df1["max"]=df1['max'].astype(int)
    

    我得到这个错误:

    ValueError: invalid literal for long() with base 10: '312.72857666015625'
    

    我试着绕过最大值,然后像这样转换:

    df1[['max']] = df1[['max']].apply(lambda x: pd.Series.round(x, 2))
    

    我得到这个错误:

    TypeError: ("can't multiply sequence by non-int of type 'float'", u'occurred at index max')
    

    在皮顿大熊猫身上有简单的方法吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   BENY    7 年前

    如果您只想转换 str int

    s.astype(float).astype(int)
    Out[213]: 
    0    312
    dtype: int32