YatShan
YatShan

Reputation: 444

How to load .npy file contents into pandas dataframe?

I have data files in .npy format and I want to load it to pandas dataframe library, in order to further processing.

I tried read_csv method of pandas library, which I used in other scripts to load files.

import pandas as pd
df = pd.read_csv('Frequency.npy')

Following error is thrown

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

Upvotes: 2

Views: 4556

Answers (1)

Alexandre B.
Alexandre B.

Reputation: 5502

You can try numpy.load with pd.DataFrame constructor:

pd.DataFrame(np.load("file.npy"))

Full example:

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     4
# 1     1     1
# 2     2     0
# 3     3     0
# 4     4     4
# 5     5     8
# 6     6     5
# 7     7     8
# 8     8     0
# 9     9     1

# Save .npy file
np.save("temp", df)

# Load .npy file as DataFrame
df_2 = pd.DataFrame(np.load("temp.npy"), columns=["col1", "col2"])

Upvotes: 4

Related Questions