Skip to content Skip to sidebar Skip to footer

Pandas - Multiple Data Frame Multiple Column Conditional Check And Assignment

How do I check if data in a column is equal to any data in another column in a different data frame? Here is some df_out1 data: 'LN' 'E' 'DATE' SMITH H '4-2-2000' H

Solution 1:

Use .isin() method to locate those rows

df_out1.loc[df_out1['E'].isnull() & df_out1['LN'].isin(ef_file_in['LN'])]

Edit: after seeing your data, I think I get what you want

import pandas as pd
df_out1 = pd.DataFrame({'LN':['SMITH','JONES','HARPER'],
                     'E': ['H',np.nan,'F'],
                   'DATE':['4-2-2000', '2-9-2018', '1-1-2000']})

ef_file_in = pd.DataFrame({'LN':['PERRY','JONES','SMITH'],'E': ['F','H','B']})

# merge the two dataframes to get all in one
df_out1 = pd.merge(df_out1,ef_file_in,on='LN',how='left')

#       LN  E_x DATE    E_y# 0 SMITH   H   4-2-2000    B# 1 JONES   NaN 2-9-2018    H# 2 HARPER  F   1-1-2000    NaN# Now E has changed to E_x and you have a new column E_y form ef_file_in# get a new column E = copy E_x except when null, then get E_y
df_out1['E'] = np.where(df_out1.E_x.isnull(),df_out1['E_y'],df_out1['E_x'])

# drop E_x and E_y, not useful now
df_out1.drop(columns=['E_x','E_y'],inplace=True)

# Notice that column E is not null anymore for LN=Jones#   LN      DATE        E# 0 SMITH   4-2-2000    B# 1 JONES   2-9-2018    H# 2 HARPER  1-1-2000    F 

Solution 2:

Use isin:

df_out1.loc[(df_out1['E'].isnull()) & df_out1['LN'].isin(ef_file_in['LN']),  'LN'] = 
ef_file_in['E']

Post a Comment for "Pandas - Multiple Data Frame Multiple Column Conditional Check And Assignment"