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
Just adding []
z = np.array([5,6])
df2 = pd.DataFrame( [z], index=[3], columns=["a","b"])
df2
Out[67]:
a b
3 5 6
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