0

i have a column called edition and the column values are'Paperback,– 10 Mar 2016' and i want to fetch only month and year from it

I have already tried

train_df['Edition_Year'] = train_df['Edition'].str.split(' ').str[3]

but all the values are not in the same length so i am not getting properly

Values in the columns are

Paperback,– 10 Mar 2016 
Hardcover,– Import, 1 Mar 2018

like this

train_df['Edition_Year'] = train_df['Edition'].str.split(' ').str[3]

I want to get two columns one for month and another for Year

Dev Khadka
  • 5,142
  • 4
  • 19
  • 33

1 Answers1

0

You can use backwards indexing:

import pandas as pd

df = pd.DataFrame(['Paperback,– 10 Mar 2016 Hardcover,– Import, 1 Mar 2018'], columns=['Edition'])

print (df)

df['Month'] = df['Edition'].str.split(' ').str[-2] 
df['Year']  = df['Edition'].str.split(' ').str[-1]

print(df)

Output:

# before
                                             Edition
0  Paperback,– 10 Mar 2016 Hardcover,– Import, 1 ...

# after
                                             Edition Month  Year
0  Paperback,– 10 Mar 2016 Hardcover,– Import, 1 ...   Mar  2018

Read more: Understanding slice notation

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69