我有.npy格式的数据文件,我想将其加载到pandas数据框库中,以便进行进一步处理。
我尝试了pandas库的read_csv方法,该方法在其他脚本中用于加载文件。
import pandas as pd
df = pd.read_csv('Frequency.npy')
引发以下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 702, in parser_f
return _read(filepath_or_buffer, kwds)
File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 429, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 895, in __init__
self._make_engine(self.engine)
File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 1122, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/anaconda3/lib/python3.7/site-packages/pandas/io/parsers.py", line 1853, in __init__
self._reader = parsers.TextReader(src, **kwds)
File "pandas/_libs/parsers.pyx", line 542, in pandas._libs.parsers.TextReader.__cinit__
File "pandas/_libs/parsers.pyx", line 782, in pandas._libs.parsers.TextReader._get_header
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 0: invalid start byte
答案 0 :(得分:1)
Numpy提供处理load
个文件的save
(doc)和.npy
(doc)方法。
这是一个基本示例:
import numpy as np
import pandas as pd
df = pd.DataFrame({"col1": np.arange(10), "col2": np.random.randint(0,10,(10))})
print(df)
# col1 col2
# 0 0 7
# 1 1 1
# 2 2 7
# 3 3 9
# 4 4 6
# 5 5 7
# 6 6 3
# 7 7 5
# 8 8 0
# 9 9 4
# Save .npy file
np.save("temp", df)
# Load .npy file
y = np.load("temp.npy")
print(y)
# [[0 7]
# [1 1]
# [2 7]
# [3 9]
# [4 6]
# [5 7]
# [6 3]
# [7 5]
# [8 0]
# [9 4]]