Pandas从python中的date string列获取日值

2024-05-13 19:53:50 发布

您现在位置:Python中文网/ 问答频道 /正文

读取数据集:

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

数据的外观如下:

^{pr2}$

日期-时间列有问题:

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日的所有天数,具体取决于月份。在

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

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('-')

熊猫数据帧有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]) 十九

到目前为止还不错。。在

现在,我先用月份过滤日期,然后尝试访问第二个逗号后的值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列中第2个逗号后的所有值。 请告诉我如何访问“新”日第3列中的日期值

vdf.iloc[:,:]

Tags: indfnewdatetimeyearpdprint
3条回答

我面临着同样的问题,用下面的代码解决了:

df['Date_Time'].dt.day 

试试看对你有用。 最棒的部分是你已经注意到了转换到datetime,甚至在你导入的时候。 现在你只需要用日期仅此而已。在

df['Date_Time'].dt.day 

我接受@edchums的回答,他们不辞辛劳地运行查询,并解释如何从python中的简单日期-时间列中提取day和其他类似的项目。在

一个伟大的回答值得全场起立鼓掌!在

这个问题很令人困惑,但我想你应该按月份过滤==12。在

如果您想对每个月执行某些操作,例如count或获取唯一值,可以使用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

相关问题 更多 >