beta
beta

Reputation: 5696

create new column based on other column but stripping

I have a pandas DataFrame with an id column looking like this:

id                
A2015        
B2016         
C2017         

I want two new columns as follows:

id         year       name  
A2015      2015       A Q
B2016      2016       B Q
C2017      2017       C Q

so the year column should take the four last characters of the id column and the name column should take all but the last four characters and add Q.

How can this be achieved?

Upvotes: 0

Views: 352

Answers (2)

sachin saxena
sachin saxena

Reputation: 976

    df['year'] = df['id'].apply(lambda v : v[1:])
    df['name'] = df['id'].apply(lambda v : v[0] + ' Q')

lambda functions are generally faster

Upvotes: 1

Zero
Zero

Reputation: 77027

You could get these using string methods.

Get year by taking part of id string

In [60]: df['year'] = df['id'].str[1:]

And, get name by taking last four characters of id string and adding ' q'

In [61]: df['name'] = df['id'].str[:-4] + ' Q'

In [62]: df
Out[62]:
      id  year name
0  A2015  2015  A Q
1  B2016  2016  B Q
2  C2017  2017  C Q

Upvotes: 2

Related Questions