Home > other >  Python using the apply and lambda to add a new column of data, the value will have square brackets,
Python using the apply and lambda to add a new column of data, the value will have square brackets,

Time:11-26

Please look at the following example,

List1=[[' zhang, 80, 'male'], [' bill ', 70, 'female'], [' Cathy ', 40 'female']]
Df1=pd. DataFrame (list1, columns=[' name ', 'points',' gender '])
Df1

Name mark sex
0 zhang 80 male
1 bill 70 female
2 fifty and 40 women


List2=[[80, 'good'], [70, 'good'], [60, 'qualified'], [50, 'bad'], [40, 'bad']]
Df2=pd. DataFrame (list2, columns=[' performance ', 'evaluation'])
Df2

Performance evaluation of
0 80 outstanding
1 70 good
2 60 qualified
Three 50 poor
Four 40


Df1 [' evaluation ']=df1. Apply (lambda x: df2 [df2 [' scores']==x [' scores']] [r]. 'evaluation' values, the axis=1)
Df1

Name gender evaluation score
0 zhang 80 male [good]
1 bill 70 female [good]
2 fifty 40 female [bad]

Question: there is a problem bothering me, is the example above, I would like to "evaluate" column is added in the list of df1,
I through the apply and lambda functions obtained from the df2, but added the data with "[]", "[good] [good] [bad]"
How do I approach the same, or use the apply and lambda, how can you as long as the value, not square brackets,

CodePudding user response:

 
Df1 [' evaluation ']=df1. Apply (lambda x: df2 [df2 [' scores']==x [' scores']] [' evaluation '] values [0], the axis=1)

CodePudding user response:

. Values [0] 
  • Related