简体   繁体   中英

Converting numpy ndarray into pandas dataframe with column names and types

Edit: As explained below in @floydian's comment, the problem was that calling a = np.array(a, dtype=d) creates an a double array which was causing the problem.

I am aware that this has been already asked multiple times, and in fact am looking at Creating a Pandas DataFrame with a numpy array containing multiple types answer right now. But I still seem to have a problem while converting. It must be something very simple that I am missing. I hope that someone can be so kind and point it out. Sample code below:

import numpy as np
import pandas as pd

a = np.array([[1, 2], [3, 4]])
d = [('x','float'), ('y','int')]
a = np.array(a, dtype=d)

# Try 1
df= pd.DataFrame(a)
# Result - ValueError: If using all scalar values, you must pass an index

# Try 2
i = [1,2]
df= pd.DataFrame(a, index=i)
# Result - Exception: Data must be 1-dimensional

I would define the array like this:

a = np.array([(1, 2), (3, 4)], dtype=[('x','float'), ('y', 'int')])
pd.DataFrame(a)

gets what you want.

One option to separate it after the fact could be eg

pd.DataFrame(a.astype("float32").T, columns=a.dtype.names).astype({k: v[0] for k, v in a.dtype.fields.items()})

Out[296]: 
     x  y
0  1.0  3
1  2.0  4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM