Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge sparse questions in multiple columns from questionnaire – Pandas

I have a pandas dataframe like this:

Q1 | Q2 | Q3 | Q4
   | 2  |    | 
1  |    |    |
   |    | 3  | 
   | 2  |    |
   |    |    | 4  

My desired output is:

Q
_
2
1
3
2
4
 

How can I do it easily in Pandas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use stack

df.stack().reset_index(drop=True).to_frame().rename({0:'Q'},axis=1)

    Q
0  2.0
1  1.0
2  3.0
3  2.0
4  4.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading