Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python/pandas/one dimensional dataframe

Tags:

python

pandas

Creating a 2 dimensional dataframe works fine:

y = np.array([[1,2],[3,4]])
df = pd.DataFrame( y, index=[1,2], columns=["a","b"] )
print (df)

But if I try to create a one dimensional dataframe I get an error message:

z = np.array([5,6])
df2 = pd.DataFrame( z, index=[3], columns=["a","b"])
print (df2)

Error message: Shape of passed values is (1, 2), indices imply (2, 1)

I tried:

z = np.array([[5],[6]])

But I get the same error message.

The reason I might want to create a one dimensional dataframe is so I can append a single row to an existing dataframe. It wont let me append a list or an array so I have to turn it into a dataframe first. But I cant do that either

I am using anaconda

like image 926
R. Emery Avatar asked Sep 07 '25 13:09

R. Emery


1 Answers

Just adding []

z = np.array([5,6])
df2 = pd.DataFrame( [z], index=[3], columns=["a","b"])
df2
Out[67]: 
   a  b
3  5  6
like image 159
BENY Avatar answered Sep 10 '25 03:09

BENY