close

How to remove nan value while combining two column in Panda Data frame?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to remove nan value while combining two column in Panda Data frame in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to remove nan value while combining two column in Panda Data frame?

  1. How to remove nan value while combining two column in Panda Data frame?

    If you want a solution that doesn't require referencing df twice or any of its columns explicitly:
    df.bfill(axis=1).iloc[:, 0]

  2. remove nan value while combining two column in Panda Data frame

    If you want a solution that doesn't require referencing df twice or any of its columns explicitly:
    df.bfill(axis=1).iloc[:, 0]

Method 1

You can use combine_first or fillna:

print df['feedback_id'].combine_first(df['_id'])
0    568a8c25cac4991645c287ac
1    568df45b177e30c6487d3603
2    568df434832b090048f34974
3    568cd22e9e82dfc166d7dff1
4    568df3f0832b090048f34711
5    568e5a38b4a797c664143dda
Name: feedback_id, dtype: object

print df['feedback_id'].fillna(df['_id'])
0    568a8c25cac4991645c287ac
1    568df45b177e30c6487d3603
2    568df434832b090048f34974
3    568cd22e9e82dfc166d7dff1
4    568df3f0832b090048f34711
5    568e5a38b4a797c664143dda
Name: feedback_id, dtype: object

Method 2

If you want a solution that doesn’t require referencing df twice or any of its columns explicitly:

df.bfill(axis=1).iloc[:, 0]

With two columns, this will copy non-null values from the right column into the left, then select the left column.

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read