0

I have dataframe as below

Slno Name_x  Age_x   Sex_x Name_y  Age_y   Sex_y
0    1      A     27    Male      A     32    Male
1    2      B     28  Female      B     28  Female
2    3      C      8  Female      C      1  Female
3    4      D     28    Male      D     72    Male
4    5      E     25  Female      E     64  Female

I need to create calculated column , difference between age, check gender match and to achieve this in one go I am using

DF3.loc[:,["Gendermatch","Agematch"]]=  pd.DataFrame([np.where(DF3["Name_x"]==DF3["Name_y"],True,False),np.where(DF3["Age_x"]-DF3["Age_y"]==0,True,False)])

and the resultant dataframe looks like as below

Slno Name_x  Age_x   Sex_x Name_y  Age_y   Sex_y  Gendermatch  Agematch
0    1      A     27    Male      A     32    Male          NaN       NaN
1    2      B     28  Female      B     28  Female          NaN       NaN
2    3      C      8  Female      C      1  Female          NaN       NaN
3    4      D     28    Male      D     72    Male          NaN       NaN
4    5      E     25  Female      E     64  Female          NaN       NaN

Resultant columns shows not a number , what wrong am I doing here?

Rajesh
  • 766
  • 5
  • 17

3 Answers3

0
DF3[["Gendermatch","Agematch"]]=  np.where(DF3["Name_x"]==DF3["Name_y"],True,False),np.where(DF3["Age_x"]-DF3["Age_y"]==0,True,False)
Rajesh
  • 766
  • 5
  • 17
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jul 11 '22 at 08:30
0
DF3[["Gendermatch","Agematch"]] = pd.DataFrame([np.where(DF3["Name_x"]==DF3["Name_y"],True,False),np.where(DF3["Age_x"]-DF3["Age_y"]==0,True,False)]).T
MoRe
  • 2,296
  • 2
  • 3
  • 23
  • I would like to achieve the output as mentioned in below link https://stackoverflow.com/questions/43667934/add-multiple-calculated-columns-to-a-pandas-dataframe-at-once – sriharsha b.s. Jul 10 '22 at 16:37
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jul 11 '22 at 08:30
0

np.where is useless, Series comparison already returns boolean Series

DF3["Gendermatch"] = DF3["Name_x"]==DF3["Name_y"]
DF3["Agematch"] = DF3["Age_x"]==DF3["Age_y"]
# or in one line
DF3["Gendermatch"], DF3["Agematch"] = (DF3["Name_x"]==DF3["Name_y"]), (DF3["Age_x"]==DF3["Age_y"])
print(DF3)

   Slno Name_x  Age_x   Sex_x Name_y  Age_y   Sex_y  Gendermatch  Agematch
0     1      A     27    Male      A     32    Male         True     False
1     2      B     28  Female      B     28  Female         True      True
2     3      C      8  Female      C      1  Female         True     False
3     4      D     28    Male      D     72    Male         True     False
4     5      E     25  Female      E     64  Female         True     False
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52