简体   繁体   中英

How to read a part of a field from a csv file using pandas?

I have a csv file with following fields

YEAR QUARTER   MONTH      WEEK           DAY
2015  20151   201501      201501W1     20150101
2015  20151   201501      201501W1     20150102
2015  20151   201501      201501W1     20150103
2015  20151   201501      201501W1     20150104
2015  20151   201501      201501W1     20150105
....

how to take only the last two digits from DAY field , ie 01,02,03,04 ...etc so that I can substitute for the remaining fields according to the day. Please suggest is there any other way we can do this?

import pandas as pd
import io

data = """
YEAR,QUARTER,MONTH,WEEK,DAY
2015,20151,201501,201501W1,20150101
2015,20151,201501,201501W1,20150102
2015,20151,201501,201501W1,20150103
2015,20151,201501,201501W1,20150104
2015,20151,201501,201501W1,20150105
"""

df = pd.read_csv(io.StringIO(data), dtype=object)

df.DAY.str[-2:]

gives you a series of the last two digits of the DAY column:

0    01
1    02
2    03
3    04
4    05
Name: DAY, dtype: object

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