I have a numpy array looking like this:
a = np.array([35,2,160,56,120,80,1,1,0,0,1])
Then I'm trying to transform that array into pandas dataframe with logic "one column-one value" like this:
columns=['age','gender','height',
'weight','ap_hi','ap_lo',
'cholesterol','gluc','smoke',
'alco','active']
values = a
df = pd.DataFrame(a,columns=columns)
This approach raises ValueError: Shape of passed values is (1, 11), indices imply (11, 11). What am I doing wrong and how to perform it in a right way?
Thanks!
Conversion of an Array into a Column Vector. This conversion can be done using a(:) operation. A(:) reshapes all elements of A into a single column vector. This has no effect if A is already a column vector.
Method #2: Using pivot() method. In order to convert a column to row name/index in dataframe, Pandas has a built-in function Pivot. Now, let's say we want Result to be the rows/index, and columns be name in our dataframe, to achieve this pandas has provided a method called Pivot.
Pandas DataFrame: transpose() function The transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied. Otherwise (default), no copy is made if possible.
You need numpy.reshape
:
columns=['age','gender','height',
'weight','ap_hi','ap_lo',
'cholesterol','gluc','smoke',
'alco','active']
a = np.array([35,2,160,56,120,80,1,1,0,0,1])
df = pd.DataFrame(a.reshape(-1, len(a)),columns=columns)
print (df)
age gender height weight ap_hi ap_lo cholesterol gluc smoke alco \
0 35 2 160 56 120 80 1 1 0 0
active
0 1
If the reshape operation is not clear to read, a more explicit way of adding a dimension to the 1d array is to use numpy.atleast_2d
pd.DataFrame(np.atleast_2d(a), columns=columns)
Or simplier add []
(but slower if really many columns):
df = pd.DataFrame([a],columns=columns)
print (df)
age gender height weight ap_hi ap_lo cholesterol gluc smoke alco \
0 35 2 160 56 120 80 1 1 0 0
active
0 1
Thanks Divakar for suggestion:
df = pd.DataFrame(a[None],columns=columns)
print (df)
age gender height weight ap_hi ap_lo cholesterol gluc smoke alco \
0 35 2 160 56 120 80 1 1 0 0
active
0 1
And another solution, thanks piRSquared:
pd.DataFrame([a], [0], columns)
Just reshape the array to what you need for the dataframe.
import pandas as pd
import numpy as np
a = np.array([35,2,160,56,120,80,1,1,0,0,1])
columns=['age','gender','height',
'weight','ap_hi','ap_lo',
'cholesterol','gluc','smoke',
'alco','active']
df = pd.DataFrame(np.reshape(a, (1,len(a))),columns=columns)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With