读取数据集:
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[:,:]
我面临着同样的问题,用下面的代码解决了:
试试看对你有用。 最棒的部分是你已经注意到了转换到datetime,甚至在你导入的时候。 现在你只需要用日期仅此而已。在
我接受@edchums的回答,他们不辞辛劳地运行查询,并解释如何从python中的简单日期-时间列中提取day和其他类似的项目。在
一个伟大的回答值得全场起立鼓掌!在
这个问题很令人困惑,但我想你应该按月份过滤==12。在
如果您想对每个月执行某些操作,例如count或获取唯一值,可以使用groupby
相关问题 更多 >
编程相关推荐