Pandas使用DataFrame进行数据分析比赛进阶之路(二):日期数据处理:按日期筛选、显示及统计数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_35512245/article/details/79791190

首先,表格的数据格式如下:

这里写图片描述

1、获取某年某月数据

data_train = pd.read_csv('data/train.csv')

# 将数据类型转换为日期类型
data_train['date'] = pd.to_datetime(data_train['date'])

# 将date设置为index
df = data_train.set_index('date')

# 获取某年的数据
print(df['2010'].head())
# 获取某月的数据
print(df['2013-11'].head())

输出结果:

           id      questions  answers
date                              
2010-10-01   1        742     1561
2010-10-02   2        400      783
2010-10-03   3        388      771
2010-10-04   4        762     1474
2010-10-05   5        821     1639
           id       questions  answers
date                                
2013-11-01  1128       3401     6858
2013-11-02  1129       2626     5467
2013-11-03  1130       2703     5557
2013-11-04  1131       3602     6941
2013-11-05  1132       3741     7312

2、获取某个时期之前或之后的数据

# 获取某个时期之前或之后的数据

# 获取2014年以后的数据
print(df.truncate(before='2014').head())
# 获取2013-11之前的数据
print(df.truncate(after='2013-11').head())
# 获取2016-02年以后的数据
print(df.truncate(before='2016-02').head())
# 获取2016-02-2年以后的数据
print(df.truncate(before='2016-02-2').head())

输出结果:

             id  questions  answers
date                                
2014-01-01  1189       2586     5576
2014-01-02  1190       3541     7175
2014-01-03  1191       3655     7395
2014-01-04  1192       2947     6099
2014-01-05  1193       2847     5935
             id  questions  answers
date                              
2010-10-01   1        742     1561
2010-10-02   2        400      783
2010-10-03   3        388      771
2010-10-04   4        762     1474
2010-10-05   5        821     1639
             id  questions  answers
date                                
2016-02-01  1950       5434    10398
2016-02-02  1951       5650    10795
2016-02-03  1952       5744    10879
2016-02-04  1953       5666    10886
2016-02-05  1954       5371    10508
             id  questions  answers
date                                
2016-02-02  1951       5650    10795
2016-02-03  1952       5744    10879
2016-02-04  1953       5666    10886
2016-02-05  1954       5371    10508
2016-02-06  1955       4296     8800

3、按某个指标显示,但不统计

# 按月显示,但不统计
df_period_M = df.to_period('M').head()
print(df_period_M)
# 按季度显示,但不统计
df_period_Q = df.to_period('Q').head()
print(df_period_Q)
# 按年度显示,但不统计
df_period_A = df.to_period('A').head()
print(df_period_A)

输出结果:

         id  questions  answers
date                           
2010-10   1        742     1561
2010-10   2        400      783
2010-10   3        388      771
2010-10   4        762     1474
2010-10   5        821     1639
         id  questions  answers
date                          
2010Q4   1        742     1561
2010Q4   2        400      783
2010Q4   3        388      771
2010Q4   4        762     1474
2010Q4   5        821     1639
         id  questions  answers
date                        
2010   1        742     1561
2010   2        400      783
2010   3        388      771
2010   4        762     1474
2010   5        821     1639

4、按某个指标显示,并且统计

# 按年统计并显示
print(df.resample('AS').sum().to_period('A'))
# 按季度统计并显示
print(df.resample('Q').sum().to_period('Q').head())
# 按月度统计并显示
print(df.resample('M').sum().to_period('M').head())
# 按月度统计并显示
print(df.resample('W').sum().to_period('W').head())

输出结果:

          id  questions  answers
date                            
2010    4278      74363   153006
2011  100375     535290  1091651
2012  234423     862831  1718434
2013  367190    1179155  2320421
2014  500415    1487677  2876611
2015  633640    1734023  3368264
2016  698810    1808649  3476335
          id  questions  answers
date                             
2010Q4   4278      74363   153006
2011Q1  12375     105858   217767
2011Q2  20748     127873   260836
2011Q3  29394     144424   293853
2011Q4  37858     157135   319195
          id  questions  answers
date                             
2010-10   496      22218    44882
2010-11  1395      25418    52841
2010-12  2387      26727    55283
2011-01  3348      31502    65477
2011-02  3850      33240    67627
           id  questions  answers
date                                          
2010-09-27/2010-10-03    6       1530     3115
2010-10-04/2010-10-10   49       4869     9636
2010-10-11/2010-10-17   98       5079    10344
2010-10-18/2010-10-24  147       5361    10847
2010-10-25/2010-10-31  196       5379    10940

附录:日期类型截图

这里写图片描述

猜你喜欢

转载自blog.csdn.net/sinat_35512245/article/details/79791190