Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single column dataframe containing 1D lists using a numpy 2D array

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?

like image 720
Achintha Ihalage Avatar asked Mar 03 '23 10:03

Achintha Ihalage


1 Answers

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]
like image 111
Anshul Avatar answered Apr 07 '23 02:04

Anshul