Python 自动化将浮点值转换为整数值的管道
我正在处理来自房价的Kaggle竞争数据。我只是尝试将所有具有浮点值的列转换为整数。代码显示了一些清理,然后显示了我正在尝试做什么。实际上只有一列有浮点值,但这是管道的一部分,所以我试图获得一种自动方法,基本上在数据帧中获取浮点值,并将它们作为整数值吐回正确的位置。我已经试了两天了,但还是打不开!下面的代码不起任何作用。。。它不会改变值。注释掉的代码在抛出错误时不起作用:AttributeError:'numpy.float64'对象没有属性'append' 这是该系列的外观:Python 自动化将浮点值转换为整数值的管道,python,Python,我正在处理来自房价的Kaggle竞争数据。我只是尝试将所有具有浮点值的列转换为整数。代码显示了一些清理,然后显示了我正在尝试做什么。实际上只有一列有浮点值,但这是管道的一部分,所以我试图获得一种自动方法,基本上在数据帧中获取浮点值,并将它们作为整数值吐回正确的位置。我已经试了两天了,但还是打不开!下面的代码不起任何作用。。。它不会改变值。注释掉的代码在抛出错误时不起作用:AttributeError:'numpy.float64'对象没有属性'append' 这是该系列的外观: 0
0 2003.0
1 1976.0
2 2001.0
3 1998.0
4 2000.0
...
1454 2004.0
1455 1999.0
1456 1978.0
1457 1941.0
1458 1950.0
Name: GarageYrBlt, Length: 1459, dtype: float64
以下是我正在尝试的代码:
# Sorts out na values
for col in none_cols:
data[col].fillna('None', inplace=True)
for col in zero_cols:
data[col].fillna(0, inplace=True)
for col in mode_cols:
data[col].fillna(data[col].mode()[0], inplace=True)
# Sorts out categorical and numerical columns
lc_cat_cols = [ccol for ccol in data.columns if data[ccol].nunique() < 10 and data[ccol].dtype == 'object']
int_cols = [ncol for ncol in X.columns if X[ncol].dtype == 'int64']
for col, colSeries in data.iteritems():
if colSeries.dtype == 'float64':
for k, v in colSeries.iteritems():
colSeries[k] = v
#colSeries[k].append(int(v))
#整理na值
对于无列中的列:
数据[col].fillna('None',inplace=True)
对于零列中的列:
数据[col].fillna(0,原地=真)
对于模式_cols中的col:
数据[col].fillna(数据[col].mode()[0],inplace=True)
#对分类列和数字列进行排序
lc_cat_cols=[ccol for ccol in data.columns if data[ccol].nunique()<10和data[ccol].dtype=='object']
int_cols=[ncol代表X.columns中的ncol如果X[ncol].dtype=='int64']
对于col,data.iteritems()中的colSeries:
如果colSeries.dtype==“float64”:
对于colSeries.iteritems()中的k,v:
colSeries[k]=v
#colSeries[k].追加(int(v))
您可以执行以下操作:
for i in df.columns:
if df.dtypes[i]=='float64':
df[i]=df[i].astype(int)
您是否尝试过:df.column\u name.astype(int)?