I want to create a new single column pandas dataframe using a 2D numpy array. Apparently, each row should contain 1D lists. Following is a simplified reproducible example.
import pandas as pd
import numpy as np
arr = np.ones((4,3)) # could be any 2D array
What I want is,
lists
0 [1, 1, 1]
1 [1, 1, 1]
2 [1, 1, 1]
3 [1, 1, 1]
Now, df = pd.DataFrame(arr, columns=['lists'])
gives the error,
ValueError: Shape of passed values is (4, 3), indices imply (4, 1)
And df = pd.DataFrame(list(arr), columns=['lists'])
gives the error,
ValueError: 1 columns passed, passed data had 3 columns
Finally, df = pd.DataFrame(arr.flatten(), columns=['lists'])
gives a wrong dataframe with all cells having a scalar 1
.
How do I get what I want?
data = {"lists": list(arr)}
df = pd.DataFrame(data, columns=['lists'])
print(df)
Output:
lists
0 [1.0, 1.0, 1.0]
1 [1.0, 1.0, 1.0]
2 [1.0, 1.0, 1.0]
3 [1.0, 1.0, 1.0]
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