2

Suppose I have a dataframe as follows

  C1 C2
0  A B
1  C NaN
2  E F
3  G H

how can I merge the two columns into one using pandas

output

  new
0  A
1  B
2  C
3  NaN
4  E
5  F
6  G
7  H
2
  • How did D suddenly appear? Commented Apr 19, 2018 at 10:16
  • 1
    Sorry, my bad. D was to be replaced with NaN. @jpp Commented Apr 19, 2018 at 10:19

1 Answer 1

3

Use DataFrame constructor with converted DataFrame to numpy array:

df = pd.DataFrame({'new':df.values.ravel()})

Or stack with reset_index:

df = df.stack(dropna=False).reset_index(drop=True).to_frame('new')
print (df)
   new
0    A
1    B
2    C
3  NaN
4    E
5    F
6    G
7    H
Sign up to request clarification or add additional context in comments.

1 Comment

Sure, once my rep goes above 15 i will.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.