python 3.x - take the year from date type in a pandas dataframe column -
this question has answer here:
i have year data in pandas dataframe following:
0 06/09/1937 1 22/11/1972
and extract year data:
0 1937 1 1972
my code:
features["year"] = df["birth_date"].str.split('/',2) features["year"] = features["year"][:2]
i got error as:
valueerror: can tuple-index multiindex
then tried
features["year"] = [x[2] x in features["year"]]
typeerror: 'float' object not subscriptable
i use python 3. tell me reasons these 2 errors , how correct them? in advance.
you need:
features["year"] = df["birth_date"].str.split('/',2) features["year"] = features["year"].str[:2]
Comments
Post a Comment