Method for pandas to process the data of a column in dataFrame

  • 2021-07-10 20:14:03
  • OfStack

Background: dataFrame data, want to do logical processing on a column, generate a new column, or overwrite the value of the original column

df in the following examples is the data of pandas. DataFrame ()

1. Add a new column or change the value of a column


df[" Column name "]= Value 

If the value is a fixed value of 1, all values of this column in dataFrame are this data

2. Work with a column


df[" Column name "]=df.apply(lambda x: Method name (x, Insert parameters 2),axis=1)

Description:

1. The method name is a separate method name, which can handle the incoming x data

2. x is the data of every 1 row, which is used as the input parameter of the method 1; Data in x can be retrieved using "x. Column Name."

3. Enter parameter 2 as other parameters required by the method, and do not need to write

4. axis=1, which means that one row of data is taken for processing every time, and it is processed by row

Examples are as follows:

Assign a value to the new column 1 or 0 depending on whether the column title contains a specific word


bugInfo['IntegrationTest'] = bugInfo.apply(lambda x: self.bug_rule(x, "IntegrationTest"), axis = 1)
 
def bug_rule(self, frame, type): #  Process the data in the list and update to the sql In the database 
  result = "0"
  if type == "SmokeTest": #  Smoke test 
    if re.search("^\[ Smoke \]|\[ Smoke test \]| "Smoke" | [Smoke test] ", frame["title"]):
      result = "1"
  elif type == "InterfaceTest": #  Interface test 
    if re.search("^\[ Interface \]|\[ Interface test \]| "Interface" | "Interface Test" ", frame["title"]):
      result = "1"
  elif type == "IntegrationTest": #  Integration test 
    if self.IntegrationTime != "" and self.IntegrationTime == frame["created_time"]:
      result = "1"
  return result

Related articles: