Pandas从python中的日期字符串列获取日期值

时间:2016-12-21 17:36:12

标签: python string date pandas dataframe

阅读数据集:

visits=   pd.read_csv('tracker.csv', low_memory=False, parse_dates=     
['Date_Time'])
df= pd.DataFrame(visits)

这是数据的外观:

print(df.head(n=1))
Date_Time       IPAddress  Visitors      OS        Browser  \
0 2016-10-18 12:57:45  104.236.233.18      1001  Mac OS  Google Chrome   
Browser_Version Location                     Referrer     PageID  
0    39.0.2171.95      NaN  http://www.puneetmathur.in/  index.php  

问题在于Date_Time列:

import datetime
df['new_date'] = [d.date() for d in df['Date_Time']]
df['new_time'] = [d.time() for d in df['Date_Time']]
df['year'] = pd.DatetimeIndex(df['new_date']).year
df['month'] = pd.DatetimeIndex(df['new_date']).month

目的是获得月份= 12,所有天数从1到30或31或28,具体取决于月份。

转换为下面的字符串并拆分值以在拆分后访问DAY值:

strdt=str(df.new_date)
df['new_date']=df['new_date'].astype(str)
df['new_date']=df.new_date.apply(str)
type(df.new_date)
df['new_day']=df.new_date.str.split('-')

Pandas Dataframe有超过1000行,所以不是问题:

print(df.new_day)
print(df.new_day)
0       [2016, 10, 18]
1       [2016, 10, 18]
2       [2016, 10, 18]
3       [2016, 10, 18]
4       [2016, 10, 18]
5       [2016, 10, 18]
6       [2016, 10, 19]
7       [2016, 10, 19]
8       [2016, 10, 19]
9       [2016, 10, 19]
10      [2016, 10, 19]
11      [2016, 10, 19]
12      [2016, 10, 19]
13      [2016, 10, 19]
14      [2016, 10, 19]
15      [2016, 10, 19]
16      [2016, 10, 19]
17      [2016, 10, 19]
18      [2016, 10, 20]
19      [2016, 10, 20]
20      [2016, 10, 20]

我想在第二个逗号两位数后访问第三个值     打印(DF [' new_day'] [6] [2])     19

到目前为止一直很好......

我现在首先使用Month过滤Date,然后尝试使用以下代码访问第二个逗号之后的值2位数值:

value_list = [12]
vdf= pd.DataFrame(df[df.month.isin(value_list)])
print(vdf[:][:].head(n=1))
print(vdf[:][:].head(n=1))
Date_Time     IPAddress  Visitors          OS       Browser  \
2836 2016-12-11 01:25:25  66.102.8.217      3955  Search Bot  Apple Safari   
Browser_Version                Location                     Referrer  \
2836               9  Florida, United States  http://www.puneetmathur.in/   

PageID    new_date  new_time  year  month         new_day  
2836  index.php  2016-12-11  01:25:25  2016     12  [2016, 12, 11]  

当我尝试访问第二个值时,它会给出奇怪的输出:

vdf['new_day'][:][:2].str.split('-')
Out[250]: Series([], Name: new_day, dtype: object)

以下也不能在第二个逗号后给出new_day第3列中的所有值。 请告诉我如何在new_day的第3列中访问DAY值

vdf.iloc[:,:]

3 个答案:

答案 0 :(得分:2)

我面临同样的问题,使用以下代码:

df['Date_Time'].dt.day 

尝试它应该适合你。 很棒的部分是,即使您导入,您已经处理了转换为datetime的问题。 现在你只需要使用dt.day来访问这一天。

答案 1 :(得分:0)

问题非常混乱,但我认为您希望按月== 12过滤。

如果您想每个月做一些事情,例如计数或获取唯一值,您可以使用groupby

import pandas as pd
import numpy as np
import io

temp=u'''Date_Time,IPAddress,Visitors,OS,Browser
2016-10-18 12:57:45,104.236.233.1,1001,Mac OS1,Google Chrome
2016-10-17 12:57:45,104.236.233.2,1002,Mac OS2,Google Chrome
2016-11-16 12:57:45,104.236.233.3,1003,Mac OS3,Google Chrome
2016-11-15 12:57:45,104.236.233.3,1004,Mac OS4,Google Chrome
2016-12-16 12:57:45,104.236.233.5,1005,Mac OS5,Google Chrome
2016-12-15 12:57:45,104.236.233.6,1006,Mac OS6,Google Chrome
'''
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), parse_dates=['Date_Time'])

# Filter month equal 12
df[df['Date_Time'].dt.month == 12]

            #~ Date_Time      IPAddress  Visitors       OS        Browser
#~ 4 2016-12-16 12:57:45  104.236.233.5      1005  Mac OS5  Google Chrome
#~ 5 2016-12-15 12:57:45  104.236.233.6      1006  Mac OS6  Google Chrome

# Groupby month
gb = df.groupby(df['Date_Time'].dt.month)

# Count by month
gb.count()

 #~ Date_Time  IPAddress  Visitors  OS  Browser
#~ Date_Time                                             
#~ 10                 2          2         2   2        2
#~ 11                 2          2         2   2        2
#~ 12                 2          2         2   2        2


# Unique ip by month
gb.IPAddress.unique()

#~ Date_Time
#~ 10    [104.236.233.1, 104.236.233.2]
#~ 11                   [104.236.233.3]
#~ 12    [104.236.233.5, 104.236.233.6]
#~ Name: IPAddress, dtype: object

答案 2 :(得分:0)

1

我接受了来自@edchums的回答,他们不遗余力地运行查询并解释如何从python中的简单日期时间列中提取日期和其他类似项目。

一个好的答案值得起立鼓掌!