简体   繁体   中英

How to modify a series with pandas?

I have an series that is like this:

0 2019.0

1 2020.0

2 2018.0

...

And I want to end like this

0 STROS-ANULAC 2018_19

1 STROS-ANULAC 2019_20

2 STROS-ANULAC 2017_18

...

I know that I can loop trough it but I want to know if it's a better way of doing this (using pandas). Thanks in advance

IIUC, you just need to use string methods to join up the rows along the index.

the key method here will be .astype(str) to convert any numeric objects into strings.

s = pd.Series([2019,2020,2018])

s = 'STROS-ANULAC  ' + (s - 1).astype(str) + '_' + (s - 2000).astype(str)

print(s)

0    STROS-ANULAC  2018_19
1    STROS-ANULAC  2019_20
2    STROS-ANULAC  2017_18

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