python 3.x - take the year from date type in a pandas dataframe column -


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

Popular posts from this blog

'hasOwnProperty' in javascript -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

java - How to provide dependency injections in Eclipse RCP 3.x? -