Skip to content
Advertisement

My dataframe is adding columns instead of rows

I’m trying to build a dataframe using for loop, below start works perfectly:

JavaScript

And I got the correct one:

enter image description here

Then I tried to make my implemetation as below:

JavaScript

But the result I got was a horizontal dataframe, not a vertical one

enter image description here

Even the data in the main hedears got posted as NAN?

I tried using enforced header type declaration, but gave same result:

JavaScript

Advertisement

Answer

Use df.loc[row] instead of df[row] to set the rows.

Though I’d rather implement this using a merge instead of the loops:

JavaScript

Or leverage itertools to obtain the cross-product:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement