web123456

Pandas assigns a column in the dataframe according to the conditional value method to find the maximum, mean, and method of each row.

1. Bulkly modify a column in the dataframe

During data processing, certain operations are often done in batches of a column. For example, dataframe df needs to operate the column name "values" greater than or equal to 30 and set to 1, and less than 30 and set to 0. You can use the dataframe's apply function to implement it in this way. The specific implementation code is as follows:

def fun(x):
     if x >= 30:
         return 1
     else:
         return 0

 values= feature['values'].apply(lambda x: fun(x))
 #If you need to assign changes to the column of the original feature, you can perform an assignment
 feature['values']=values
 # Or directly modify it and assign the value.
 feature['values']= feature['values'].apply(lambda x: fun(x))

The specific logic can be modified to implement the fun function, but selecting columns according to certain conditions is not the implementation method. If you have any requirements, please Baidu.

2. Find the maximum value, minimum value, mean value, sum and other operations in a datafram line

In some data processing, it is necessary to find the maximum value, minimum value, mean value and other operations of the numerical column in a row, which can be implemented as follows:

#If you need to operate on multiple columns, for example, the original DF format is sid, math, Chinese, PE, it represents a student's score, such as finding the highest score, average score, total score, etc.
 # Since sid is the student number in a row, we do not need to select the column to find the maximum value or the minimum value, so before finding the maximum value, all columns that need to find the maximum value are selected
 #Raw data: 1,88,89,87
 # 2,90,98,94
 # 3,89,89,90
 feature["max"]=feature[["math","Chinese","PE"]]].max(axis=1)
 feature["mean"]=feature[["math","Chinese","PE"]]].mean(axis=1)
 feature["sum"]=feature[["math","Chinese","PE"]]].sum(axis=1)