A recent development requirement requires pandas to be implemented:
Compare the two columns row by row, select the larger value of each row and two columns to add to the third column
After looking through it, it seems that there is no similar function, so I can't make the wheels myself, just upload the code and comments.
# The values that need to be compared are value_x and value_y# The column name of the new home is value_final# 1. Set a flag with the value of value_y-value_x, which means positive means y is larger, and negative means x is largerdf_test['value_flag'] = df_test['Value_y'] - df_test['Value_x'] # 2. Obtain the larger part of y and the larger part of x respectivelydf_test_bigger = df_test[df_test['value_flag'] >= 0].copy() df_test_litter = df_test[df_test['value_flag'] < 0].copy() # 3. Assign final values separatelydf_test_bigger['Value_Final'] = df_test_bigger['Value_y'] df_test_litter['Value_Final'] = df_test_litter['Value_x'] # 4. Use the concat function to aggregate itdf_test_1 = ([df_test_bigger, df_test_litter])
Supplement: Pandas technique---the two columns add up to form a new column (eval)
as follows:
('New fields=Fields1+Fields2',inplace=True) ("""New fields1=Fields1+Fields2 New fields2=Fields1+Fields2 New fields3=Fields1+Fields2""",inplace=True)
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.