Home > other >  Python in the replacement process, how to say all the rest of the data with the same value
Python in the replacement process, how to say all the rest of the data with the same value

Time:11-19

Excuse me when I was in Python processing car age segmentation,
Use the
Df3 [" old section "]=df3 [" old "]
Df3 [r]. "old section" replace ({0:1 year under the "", 1:1 to 2 years, 2:2-3 years, 3:" 3-4 years ", 4: "four to five years," 5: "five to six years," 6: "six to seven years," 7:7-8 years, 8:8 to 9 years, 9: "9 to 10 years,"??? ,}, inplace=True)
What in? Department said the rest of the data is no replacement for "more than 10 years," thank you!

CodePudding user response:

Can try to write a function and then use the apply


 def age_flag (df) : 
Age_flag_dict={0:1 year under the "", 1:1 to 2 years, 2:2-3 years, 3:" 3-4 years ", 4: "four to five years," 5: "five to six years," 6: "six to seven years," 7:7-8 years, 8:8 to 9 years, 9: "9-10"}
Age=df (" old ")
If the age in age_flag_dict:
Return age_flag_dict [age]
Esel:
Return 'more than 10 years'

Df3 [" old section "]=df3. Apply (age_flag, axis=1)

CodePudding user response:

Thank you
  • Related