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?
CodePudding user response:
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
CodePudding user response:
here is another way to do it
df.fillna(method='bfill', axis=1)['Q1'].astype(int).to_frame().rename(columns={'Q1':'Q'})
Q
0 2
1 1
2 3
3 2
4 4