Home > Software engineering >  How to assign values to the rows of a data frame which satisfy certain conditions?
How to assign values to the rows of a data frame which satisfy certain conditions?

Time:11-08

I have two data frames:

df1 = pd.read_excel("test1.xlsx")
df2 = pd.read_excel("test2.xlsx")

I am trying to assign values of df1 to df2 where a certain condition is met (Column1 is equal to Column1 then assign values of ColY to ColX).

df1.loc[df1['Col1'] == df2['Col1'],'ColX'] = df2['ColY'] 

This results in an error as df2['ColY] is the whole column. How do i assign for only the rows that match?

CodePudding user response:

You can use numpy.where:

import numpy as np 

df1['ColX'] = np.where(df1['Col1'].eq(df2['Col1']), df2['ColY'], df1['ColX'])

CodePudding user response:

Since you wanted to assign from df1 to df2 your code should have been

df2.loc[df1['Col1']==df2['Col2'],'ColX']=df1.['ColY']

The code you wrote won't assign the values from df1 to df2, but from df2 to df1. And also if you could clarify to which dataframe ColX and ColY belong to I could help more(Or does both dataframe have them??). Your code is pretty much right!!! Only change the df1 and df2 as above.

  • Related