简体   繁体   English

如何使用熊猫从csv文件读取字段的一部分?

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

I have a csv file with following fields 我有一个带有以下字段的csv文件

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. 如何只取DAY字段的最后两位数字,即01,02,03,04 ...等,以便我可以根据日期替换其余字段。 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: 给您DAY列的最后两位数字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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