[英]create new column based on other column but stripping
I have a pandas DataFrame with an id
column looking like this: 我有一个带有id
列的pandas DataFrame看起来像这样:
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
. 因此year
列应采用id
列的最后四个字符,而name
列应采用除最后四个字符以外的所有字符,并加上Q
How can this be achieved? 如何做到这一点?
You could get these using string methods. 您可以使用字符串方法获得这些。
Get year
by taking part of id
string 参加id
字符串获取year
In [60]: df['year'] = df['id'].str[1:]
And, get name
by taking last four characters of id
string and adding ' q'
并且,通过获取id
字符串的最后四个字符并添加' q'
获取name
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
df['year'] = df['id'].apply(lambda v : v[1:])
df['name'] = df['id'].apply(lambda v : v[0] + ' Q')
lambda functions are generally faster lambda函数通常更快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.