简体   繁体   中英

How do i slice a series in python?

How do i slice a series to retrieve the last 3 characters?

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.

This will create a series with the last 3 letters of the Names column of your data dataframe.

last_three = data.Names.apply(lambda x: x[-3:])

You were almost there. You only missed to call .str on your series.

>>> new_series = data["Names"].str[-3:]
>>> print(new_series)
0    ROU
1    WAM
2    AVE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM