Question
Trying to change a range of values in pandas dataframe into a new value. Why isn't this working? The code is below. #converting customer_income ranges
Trying to change a range of values in pandas dataframe into a new value. Why isn't this working? The code is below.
#converting customer_income ranges into categorical variables df1.replace(to_replace = "0 - 20000", value = "I1") df1.replace(to_replace = "20001 - 40000", value = "I2") df1.replace(to_replace = "40001 - 60000", value = "I3") df1.replace(to_replace = "60001 - 80000", value = "I4") df1.replace(to_replace = "80001 - 100000", value = "I5") df1.replace(to_replace = "100001 - 120000", value = "I6") df1.replace(to_replace = "120001 - 140000", value = "I7") df1.replace(to_replace = "140001 - 160000", value = "I8") df1.replace(to_replace = "160001 - 180000", value = "I9") df1.replace(to_replace = "180001 - 20000", value = "I10") df1.replace(to_replace = "200000+", value = "I11")
#customer_age df1.replace(to_replace = "0 - 20", value = "A1") df1.replace(to_replace = "21 - 30", value = "A2") df1.replace(to_replace = "31 - 40", value = "A3") df1.replace(to_replace = "41 - 50", value = "A4") df1.replace(to_replace = "51 - 60", value = "A5") df1.replace(to_replace = "61 - 70", value = "A6") df1.replace(to_replace = "71 - 80", value = "A7") df1.replace(to_replace = "81 - 90", value = "A8") df1.replace(to_replace = "91 - 100", value = "A9") df1.replace(to_replace = "101+", value = "A10")
#purchase_price df1.replace(to_replace = "5001 - 10000", value = "P1") df1.replace(to_replace = "10001 - 15000", value = "P2") df1.replace(to_replace = "15001 - 20000", value = "P3") df1.replace(to_replace = "20001 - 25000", value = "P4") df1.replace(to_replace = "25001 - 30000", value = "P5") df1.replace(to_replace = "30001 - 35000", value = "P6") df1.replace(to_replace = "35001 - 40000", value = "P7") df1.replace(to_replace = "40001 - 45000", value = "P8") df1.replace(to_replace = "45001 - 50000", value = "P9") df1.replace(to_replace = "50001 - 55000", value = "P10") df1.replace(to_replace = "55001 - 60000", value = "P11") df1.replace(to_replace = "60001 - 65000", value = "P12") df1.replace(to_replace = "65001 - 70000", value = "P13") df1.replace(to_replace = "70001 - 75000", value = "P14") df1.replace(to_replace = "75001 - 80000", value = "P15") df1.replace(to_replace = "80001 - 85000", value = "P16") df1.replace(to_replace = "85001 - 90000", value = "P17") df1.replace(to_replace = "90001 - 95000", value = "P18")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started