在python中如何将数据帧列转换为int
我试图在python中将数据类型转换为inter 当我这样做的时候在python中如何将数据帧列转换为int,python,pandas,Python,Pandas,我试图在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' TypeError: ("can't multiply s
df1.dtypes
我明白了:
max object
我需要将df1['max']转换为int
当我这样做时:
df1["max"]=df1['max'].astype(int)
我得到这个错误:
ValueError: invalid literal for long() with base 10: '312.72857666015625'
TypeError: ("can't multiply sequence by non-int of type 'float'", u'occurred at index max')
在转换之前,我尝试将最大值四舍五入,如下所示:
df1[['max']] = df1[['max']].apply(lambda x: pd.Series.round(x, 2))
我得到这个错误:
ValueError: invalid literal for long() with base 10: '312.72857666015625'
TypeError: ("can't multiply sequence by non-int of type 'float'", u'occurred at index max')
在pyton pandas中有没有一种简单的方法可以做到这一点?如果您只想将
str
转换为int
s.astype(float).astype(int)
Out[213]:
0 312
dtype: int32
如果您只想将
str
转换为int
s.astype(float).astype(int)
Out[213]:
0 312
dtype: int32
首先尝试将其转换为str,然后转换为int:
df1[“max”]=df1['max'].astype(str).astype(int)
I仍然会出现以下错误:ValueError:long的无效文本()以10为基数:'312.72857666015625'首先尝试将其转换为str,然后转换为int:df1[“max”]=df1['max'].astype(str).astype(int)
我仍然会遇到以下错误:ValueError:literal for long()无效,以10为基数:“312.72857666015625”