gpt4 book ai didi

python - 为什么 pandas 数据帧将整数转换为 float 据类型

转载 作者:行者123 更新时间:2023-12-01 06:34:19 27 4
gpt4 key购买 nike

我有一个 csv 文件

Wed Dec 04 11:30:04 GMT+05:30 2019,20,35.0,143455434,0
Wed Dec 04 11:30:13 GMT+05:30 2019,40,25.5,null,

我想将其加载到 pandas 中并将各个列转换为我各自的数据类型。我就是这样做的

raw_df = pd.read_csv('raw.csv', dtype=str)
raw_df = raw_df.replace({'null':None, pd.np.nan: None})

这是我要转换的函数

def df_function(row):
row['timestamp'] = parse(row['timestamp'])
row['odometer'] = float(row['odometer']) + 1
row['speed'] = float(row['speed'])

if row['id'] is not None:
row['id'] = str(row['id'])

if row['error_code'] is not None:
row['error_code'] = int(row['error_code'])

return row
raw_df = raw_df.apply(df_function, axis=1)

当您打印列的数据类型时,您会发现

timestamp     datetime64[ns, tzoffset(None, -19800)]
odometer float64
speed float64
id object
error_code float64
dtype: object

error_code 是 float64,虽然它应该是 int64,这里有什么问题

最佳答案

正如 pandas documents 中提到的

The Integer NA support currently uses the capitalized dtype version, e.g. Int8 as compared to the traditional int8. This may be changed at a future date

您需要将列更改为 Int8

df = pd.DataFrame({"error_code":[1,2,5,np.nan]}) 
print(df.dtypes)

# error_code float64
# dtype: object

df["error_code"] = df["error_code"].astype("Int8")
print(df.dtypes)

输出:

error_code    Int8
dtype: object

关于python - 为什么 pandas 数据帧将整数转换为 float 据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59747299/

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