0

I want to change the value of column "MSSubClass" and "MoSold"in my dataframe "trainData" using replace method:

"MSSubClass": change all numerical value like 20 to "SC20", 30 : "SC30", 40 : "SC40", 45 : "SC45", 50 : "SC50", ...

"MoSold": change all numerical value to month, like 1 to "Jan", 2 : "Feb", 3 : "Mar", 4 : "Apr", 5 : "May", 6 : "Jun", ...

I have tried the straight forward way as followed:

trainData['MSSubClass'].replace({20 : "SC20", 30 : "SC30", 40 : "SC40", 45 : "SC45", 50 : "SC50", 60 : "SC60", 70 : "SC70", 75 : "SC75", 80 : "SC80", 85 : "SC85", 90 : "SC90", 120 : "SC120", 150 : "SC150", 160 : "SC160", 180 : "SC180", 190 : "SC190"})

trainData['MoSold'].replace({1 : "Jan", 2 : "Feb", 3 : "Mar", 4 : "Apr", 5 : "May", 6 : "Jun", 7 : "Jul", 8 : "Aug", 9 : "Sep", 10 : "Oct", 11 : "Nov", 12 : "Dec"})

I would like to know if there is a better or efficient way to do so(maybe still use replace method)

Thanks!

realllllyiyun
  • 37
  • 1
  • 4

1 Answers1

0

I don't think you should care about efficiency at all. (you can, and someone will probably answer what the most efficient way is and that is a legitimate question). What I would care about is making it pythonic and clean. for the month I would do (copied from Get month name from number):

import datetime
datetime.date(1900, monthinteger, 1).strftime('%B')

For the string, just prepend the 'SC' string to the number. Something like:

'SC{}'.format(your_string), 

That makes your code readable for anyone.

If you really want to make it pretty make a function called month_number_to_string where you wrap

def month_number_to_string(month_number):
    return datetime.date(1900, month_number, 1).strftime('%B')

And apply it to the data you want to

E.Serra
  • 1,495
  • 11
  • 14