Skip to content
Advertisement

multiple rows into single row in pandas

I wish to flatten(I am not sure whether is the correct thing to call it flatten) the columns with rows. multiple rows into single row with column change to column_rows I have a dataframe as below:

JavaScript

my current output is:

JavaScript

my expected otput:

JavaScript

from shape (4,4) to (1, 16)

Advertisement

Answer

Update let’s use the walrus operator new in Python 3.8 to create a one-liner:

JavaScript

Output:

JavaScript

Try this, using unstack, to_frame and transpose. Next, flatten the column headers using list comprehension:

JavaScript

Output:

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