Home > Blockchain >  How to convert a column of x years and y months into [12(x) y] months
How to convert a column of x years and y months into [12(x) y] months

Time:03-24

My data has a remaining_lease column where it is in x years and y months I would like to change it into [12(x) y] months Here is a picture of the data

I have tried the code below but an error keeps occuring import pandas as pd

def lease_string_to_months(time):

split_string = time.split(' ')

months = 12*int(split_string[0]) int(split_string[2])

return months

df1 = 'resale-flat-prices-based-on-registration-date-from-jan-2017-onwards.csv' # write the filepath here as a string

house_lease = pd.read_csv(df1) new_header = house_lease.iloc[0]

house_lease = house_lease[1:]

house_lease.columns = new_header

house_lease['remaining_lease'].map(lease_string_to_months)

CodePudding user response:

First - read about pd.read_csv() as well as inspect what you have in house_lease. In other words, I doubt that you need most of your transformations.

Since I don't have your cvs file, I made toy example here, split remaining_lease (string) column, converted [0]th & 2th columns to number, pushed 12*Y M to a new months column in dataframe.

import pandas as pd
d = {'remaining_lease': ['55 years 12 months', '12 years 01 months']}
df = pd.DataFrame(d)
rdf = df['remaining_lease'] \
    .str.split(r" ", expand=True) \
    .iloc[:, [0,2]] \
    .apply(pd.to_numeric)
df['months'] = 12*rdf[0]   rdf[2]

print(df)

>     remaining_lease  months
0  55 years 12 months     672
1  12 years 01 months     145
  • Related