gpt4 book ai didi

python - 类型错误 : data type not understood while parsing CSV with Pandas

转载 作者:行者123 更新时间:2023-11-28 17:55:34 25 4
gpt4 key购买 nike

当使用 Pandas 解析 CSV 文件时,包含 datetime 的列默认分配类型 object

如何确保第一列在此示例中分配了正确的类型?

import pandas as pd
import datetime as datetime

data = pd.read_csv("scans.csv")

# dtypes = {
# 'date': datetime,
# 'muscle': str,
# 'side': str,
# 'MQ(0-100)': float,
# 'MQ(raw)': int,
# 'fat': float
# }
# data = pd.read_csv("scans.csv", dtype=dtypes)

print(data.head())
print(data.dtypes)

这是控制台输出

               date      muscle side  MQ(0-100)  MQ(raw)   fat
0 12/16/2018 16:08 glutes R 99.7 154 8.6
1 12/16/2018 16:08 total R 81.8 129 17.0
2 12/16/2018 16:04 glutes L 98.1 140 10.8
3 12/16/2018 16:03 upper_back R 70.2 132 11.6
4 12/16/2018 16:02 upper_back L 77.8 136 11.4
date object
muscle object
side object
MQ(0-100) float64
MQ(raw) int64
fat float64
dtype: object

运行完整代码时出错

/Users/Developer/PycharmProjects/Sculpt/venv/bin/python /Users/Developer/PycharmProjects/Sculpt/script.py
Traceback (most recent call last):
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/core/dtypes/common.py", line 2050, in pandas_dtype
npdtype = np.dtype(dtype)
TypeError: data type not understood

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/Developer/PycharmProjects/Sculpt/script.py", line 18, in <module>
data = pd.read_csv("scans.csv", dtype=dtypes)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 685, in parser_f
return _read(filepath_or_buffer, kwds)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 457, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 895, in __init__
self._make_engine(self.engine)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 1135, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/io/parsers.py", line 1917, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 490, in pandas._libs.parsers.TextReader.__cinit__
File "/Users/Developer/PycharmProjects/Sculpt/venv/lib/python3.8/site-packages/pandas/core/dtypes/common.py", line 2054, in pandas_dtype
raise TypeError("data type not understood")
TypeError: data type not understood

最佳答案

这是一个单行解决方案:

data = pd.read_csv("scans.csv", parse_dates=['date'])

现在得到了一个不错的结果:

date         datetime64[ns]
muscle object
side object
MQ(0-100) float64
MQ(raw) int64
fat float64
dtype: object

关于python - 类型错误 : data type not understood while parsing CSV with Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58672758/

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