Home > Software engineering >  How to replace values in column when other column is not nan or replace with other?
How to replace values in column when other column is not nan or replace with other?

Time:05-14

I am new to Pandas, I am looking for solution where I can replace one column values with other columns. For eg:

Replace value of col A with the values in Col E if the values are Nan in colE, Replace with the value of ColD if not nan, else replace with the values of ColC. Can anyone please help?

CodePudding user response:

You can use .fillna for that, which basically means "use original value if it exists, otherwise use the fill value".

So, your example should look something like this:

df_merged = df.assign(A=df['A'].fillna(df['E']).fillna(df['D']).fillna(df['C'])

CodePudding user response:

What you describe looks a lot like bfill:

df = pd.DataFrame({'A': [1, None, None, None, None],
                   'B': [2, 3, None, None, None],
                   'C': [4, 5, 6, 7, None],
                   'D': [8, 9, 10, None, None],
                   'E': [11, 12, None, None, None]
                  })
#      A    B    C     D     E
# 0  2.0  2.0  4.0   8.0  11.0
# 1  3.0  3.0  5.0   9.0  12.0
# 2  6.0  NaN  6.0  10.0   NaN
# 3  7.0  NaN  7.0   NaN   NaN
# 4  NaN  NaN  NaN   NaN   NaN


order = ['A', 'E', 'D', 'C']
df['A'] = df[order].bfill(axis=1)['A']

#       A    B    C     D     E
# 0   1.0  2.0  4.0   8.0  11.0
# 1  12.0  3.0  5.0   9.0  12.0
# 2  10.0  NaN  6.0  10.0   NaN
# 3   7.0  NaN  7.0   NaN   NaN
# 4   NaN  NaN  NaN   NaN   NaN

or if you really want to replace all values of A (even the non-NaN):

order = ['E', 'D', 'C']
df['A'] = df[order].bfill(axis=1)['E']

#       A    B    C     D     E
# 0  11.0  2.0  4.0   8.0  11.0
# 1  12.0  3.0  5.0   9.0  12.0
# 2  10.0  NaN  6.0  10.0   NaN
# 3   7.0  NaN  7.0   NaN   NaN
# 4   NaN  NaN  NaN   NaN   NaN
  • Related