How can i convert column into days in csv having string values in years , months , weeks using Python Pandas?
I have done a try with python as below :
import pandas as pd
df = pd.read_csv("master_list.csv", usecols = ['Duration'])
#month manipulation starts
df[df['Duration'].fillna('').str.contains('Months') & df['Duration'].fillna('').str.len().gt(0)]
df["Duration"] = df["Duration"].str.replace("\sMonths", "")
df['Duration'] = pd.to_numeric(df['Duration'], errors='coerce').fillna(0).astype(float)
df['Duration'] *= 30.436875
#month manipulation end
df[df['Duration'].fillna('').str.contains('Weeks') & df['Duration'].fillna('').str.len().gt(0)]
df["Duration"] = df["Duration"].str.replace("\sWeeks", "")
#df['Duration'] *= 30.436875
print(df)
Sample_Data=["7 Months","1 Year","8 Weeks","3 Days"] Expected Output=["210 Days","365 Days","56 Days","3 Days"]
source https://stackoverflow.com/questions/73743504/how-can-i-convert-column-into-days-in-csv-having-string-values-in-years-months
Comments
Post a Comment