gpt4 book ai didi

python - 使用pd.read_csv(),指定数据为np.float32,但得到字符串

转载 作者:行者123 更新时间:2023-12-01 06:54:59 24 4
gpt4 key购买 nike

当我尝试从 csv 文件读取数据时。我想将“数据”列的数据类型设置为 float ,所以我的代码是这样的:

data = pd.read_csv('data.csv', index_col=0, 
parse_dates=[0],
skiprows=[0], names=['Data'],
dtype={'Data':np.float32})

但是我得到的结果仍然是字符串列表。有什么原因和解决办法吗?

[5]: data.values

[5]: array([['1188.0'],
['1377.0'],
['1279.0'],
['1461.0'],
['1146.0'],
['1287.0'],
['1259.0']], dtype=object)

最佳答案

names参数添加值:

import pandas as pd
import numpy as np
from io import StringIO

temp="""2019-01-02,1188.0
2019-01-03,1377.0
2019-01-04,1279.0
2019-01-05,1461.0
2019-01-06,1146.0
2019-01-07,1287.0
2019-01-08,1259.0"""
#after testing replace 'pd.compat.StringIO(temp)' to 'data.csv'
df = pd.read_csv(StringIO(temp),
index_col=0,
parse_dates=[0],
names=['dates','Data'],
dtype={'Data':np.float32})
<小时/>
print (df)
Data
dates
2019-01-02 1188.0
2019-01-03 1377.0
2019-01-04 1279.0
2019-01-05 1461.0
2019-01-06 1146.0
2019-01-07 1287.0
2019-01-08 1259.0

print (df.dtypes)
Data float32
dtype: object

print (df.index)
DatetimeIndex(['2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05',
'2019-01-06', '2019-01-07', '2019-01-08'],
dtype='datetime64[ns]', name='dates', freq=None)

关于python - 使用pd.read_csv(),指定数据为np.float32,但得到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58850193/

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