简体   繁体   English

Python如何获取日期令牌的第一个元素

[英]Python How to get 1st element of date token

I'm using Python 2.7 and my data looks like this: 我正在使用Python 2.7,我的数据如下所示:

import pandas as pd            
df = pd.DataFrame({ 'DateVar' : ['9/1/2013', '10/1/2013', '2/1/2014'],
                'Field' : 'foo' })   

I want to parse DateVar to create 2 new fields: a 'month' field and a 'year' field. 我想解析DateVar以创建2个新字段:“月”字段和“年”字段。

I was able to tokenize 'DateVar' via vectorized string method: 我能够通过向量化字符串方法标记'DateVar':

df.DateVar.str.split('/')

This is a little closer to what I want, so then I next tried to slice the months [9, 10, 2] using the following code: 这与我想要的有点接近,因此我接下来尝试使用以下代码对月份[9,10,2]进行切片:

df.DateVar.str.split('/')[0]

But unexpectedly, I'm getting: 但是出乎意料的是,我得到了:

['9', '1', '2013'] ['9','1','2013']

So how can I get a vector of all the months? 那么如何获得所有月份的向量?

If you only need one column, you can use: 如果只需要一列,则可以使用:

df.DateVar.str.split("/").str[0]

If you need the month and day column, use str.extract : 如果需要月和日列,请使用str.extract

import pandas as pd            
df = pd.DataFrame({ 'DateVar' : ['9/1/2013', '10/1/2013', '2/1/2014'],
                'Field' : 'foo' })   

print df.DateVar.str.extract(r"(?P<month>\d+)/(?P<day>\d+)/\d+").astype(int)

the output: 输出:

  month  day
0      9    1
1     10    1
2      2    1

It is because 这是因为

>>> df.DateVar.str.split('/')
0     [9, 1, 2013]
1    [10, 1, 2013]
2     [2, 1, 2014]

so 所以

>>> df.DateVar.str.split('/')[0]
['9', '1', '2013']
v = [x[0] for x in df.DateVar.str.split('/')]

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

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