Home > Software engineering >  Pandas Dataframe ValueError: Shape of passed values is (3, 1), indices imply (3, 3)
Pandas Dataframe ValueError: Shape of passed values is (3, 1), indices imply (3, 3)

Time:01-03

I am scraping some data into an array. I want to create a dataframe with columns for the data. The structure of array is like this:

[['Aasim khan', '2 Dec', '0'], ['Aasim khan', '3 Dec', '0'], ['Aasim khan', '5 Dec', '0']]

I am looping in the length of this array and I want to insert this data in a dataframe like this:

for i in range(len(array)):
    df = pd.DataFrame(
        array[i],
        columns=["consultant", "date", "count"]
    )

Ideally this code should work as I'm passing three values in a array[i] field for the three columns for each iteration. But I'm getting above error. Is there something I'm doing wrong?

CodePudding user response:

I think no loop necessary, pass array to DataFrame constructor:

array = [['Aasim khan', '2 Dec', '0'],
         ['Aasim khan', '3 Dec', '0'],
         ['Aasim khan', '5 Dec', '0']]

df = pd.DataFrame(array,columns=["consultant", "date", "count"])
print (df)
   consultant   date count
0  Aasim khan  2 Dec     0
1  Aasim khan  3 Dec     0
2  Aasim khan  5 Dec     0
  • Related