I have 2 arrays as below that I want to be converted to dataframe columns:
arr1 = np.array([2, 4, 6, 8])
arr2 = np.array([3, 6, 9, 12])
df_from_arr = pd.DataFrame(data=[arr1, arr2])
print(df_from_arr)
Actual Output:
0 1 2 3
0 2 4 6 8
1 3 6 9 12
Expected output:
0 1
0 2 4
1 4 6
2 6 9
3 8 12
How can I get the expected output?
Add T at the end
df_from_arr.T
Out[418]:
0 1
0 2 3
1 4 6
2 6 9
3 8 12
Or modify your input arrays
pd.DataFrame(np.hstack((arr1[:,None], arr2[:,None])))
Out[426]:
0 1
0 2 3
1 4 6
2 6 9
3 8 12
You can transpose the DataFrame.
arr1 = np.array([2, 4, 6, 8])
arr2 = np.array([3, 6, 9, 12])
df_from_arr = pd.DataFrame(data=[arr1, arr2]).T
print(df_from_arr)
0 1
0 2 3
1 4 6
2 6 9
3 8 12
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