Home > Software design >  Python merge two columns based on condition
Python merge two columns based on condition

Time:12-14

I have the following dataframe with two columns 'Department' and 'Project'.

data = [['Sales', '', 1000], ['R&D', 'Alpha', 2000], ['Purchase', 'DE', 1500],['Communication', '', 2000], ['HR', '', 10020]]
df = pd.DataFrame(data, columns = ['Department', 'Project', 'Amount'])
print(df)

   Department Project  Amount
0          Sales            1000
1            R&D   Alpha    2000
2       Purchase      DE    1500
3  Communication            2000
4             HR           10020

I want to replace the Department entry by the Project entry if the Project entry is not empty. So the dataframe looks like that:

   Department Project  Amount
0          Sales            1000
1          Alpha   Alpha    2000
2             DE      DE    1500
3  Communication            2000
4             HR           10020

How can I do this?

CodePudding user response:

Try with pandas.DataFrame.where:

df["Department"] = df["Project"].where(df["Project"].ne(''), df["Department"])

>>> df
      Department Project  Amount
0          Sales            1000
1          Alpha   Alpha    2000
2             DE      DE    1500
3  Communication            2000
4             HR           10020

CodePudding user response:

You can do this with np.where(). Kindly try:

df['Department'] = np.where(df['Project'].replace('',np.nan).isna(),df['Department'],df['Project'])

This outputs:

      Department Project  Amount
0          Sales            1000
1          Alpha   Alpha    2000
2             DE      DE    1500
3  Communication            2000
4             HR           10020

CodePudding user response:

Another way is with series.fillna on column Project with column Department

df['Department'] = df['Project'].replace('',np.nan).fillna(df['Department'])

print(df)

      Department Project  Amount
0          Sales            1000
1          Alpha   Alpha    2000
2             DE      DE    1500
3  Communication            2000
4             HR           10020
  • Related