[英]How do i slice a series in python?
How do i slice a series to retrieve the last 3 characters?如何切片系列以检索最后 3 个字符?
The series look like this:该系列看起来像这样:
1 WHITNEY ROU
2 ABBY WAM
3 YAEL AVE
I tried data["Names"][:-3] to get ROU
, WAM
and AVE
but failed to do so.我尝试使用 data["Names"][:-3] 来获取
ROU
、 WAM
和AVE
但未能这样做。
This will create a series with the last 3 letters of the Names column of your data
dataframe.这将创建一个系列,其中包含
data
dataframe 的 Names 列的最后 3 个字母。
last_three = data.Names.apply(lambda x: x[-3:])
You were almost there.你快到了。 You only missed to call
.str
on your series.您只是错过了在您的系列上调用
.str
。
>>> new_series = data["Names"].str[-3:]
>>> print(new_series)
0 ROU
1 WAM
2 AVE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.