3-订单持续时间的计算

#读取taxiod订单表
#删除练习
import pandas as pd
taxiod = pd.read_csv(r'data-sample/TaxiOD.csv',header=None) # 要加上后缀名 .csv
taxiod.columns=['VehicleNum','Stime','SLng','SLat','ELng','ELat','Etime']
taxiod
C:\Program Files (x86)\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3146: DtypeWarning: Columns (0,2,3,4,5) have mixed types.Specify dtype option on import or set low_memory=False.
  has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
VehicleNumStimeSLngSLatELngELatEtime
0VehicleNumStimeSLngSLatELngELatEtime
12222300:03:23114.1674649999999922.562468114.2252350000000122.5527500:10:48
22222300:11:33114.2271522.554167114.2292179999999922.56021700:15:19
32222300:17:13114.2313540000000122.562166114.25579822.59096700000000300:29:06
42222300:36:45114.24019622.56365114.11996522.56666800:54:42
........................
4647143694722:39:12114.00622.5481113.99622.537122:46:25
4647153694722:49:38113.99522.535113.92222.496523:13:15
4647163694723:24:24113.92122.5135113.9322.494223:30:32
4647173694723:37:09113.92822.5126113.91122.487923:49:10
4647183694723:52:18113.9122.4876NaNNaNNaN

464719 rows × 7 columns

taxiod=taxiod.drop([0])  # 删除第一行 
taxiod.index = range(len(taxiod)) # 重新排序索引
taxiod
VehicleNumStimeSLngSLatELngELatEtime
02222300:03:23114.1674649999999922.562468114.2252350000000122.5527500:10:48
12222300:11:33114.2271522.554167114.2292179999999922.56021700:15:19
22222300:17:13114.2313540000000122.562166114.25579822.59096700000000300:29:06
32222300:36:45114.24019622.56365114.11996522.56666800:54:42
42222301:01:14114.1354139999999822.575933114.16674822.60826701:08:17
........................
4647133694722:39:12114.00622.5481113.99622.537122:46:25
4647143694722:49:38113.99522.535113.92222.496523:13:15
4647153694723:24:24113.92122.5135113.9322.494223:30:32
4647163694723:37:09113.92822.5126113.91122.487923:49:10
4647173694723:52:18113.9122.4876NaNNaNNaN

464718 rows × 7 columns

taxiod=taxiod[-taxiod['ELng'].isnull()] # 删掉最后一行为空的   方法  先找到为空的 然后索引 然后去掉  然后赋值给taxiod
tmp= pd.to_datetime(taxiod['Stime'])
tmp
0        2021-03-03 00:03:23
1        2021-03-03 00:11:33
2        2021-03-03 00:17:13
3        2021-03-03 00:36:45
4        2021-03-03 01:01:14
                 ...        
464712   2021-03-03 22:08:22
464713   2021-03-03 22:39:12
464714   2021-03-03 22:49:38
464715   2021-03-03 23:24:24
464716   2021-03-03 23:37:09
Name: Stime, Length: 464717, dtype: datetime64[ns]
tmp1=pd.to_datetime(taxiod['Etime'])
tmp1
0        2021-03-03 00:10:48
1        2021-03-03 00:15:19
2        2021-03-03 00:29:06
3        2021-03-03 00:54:42
4        2021-03-03 01:08:17
                 ...        
464712   2021-03-03 22:36:53
464713   2021-03-03 22:46:25
464714   2021-03-03 23:13:15
464715   2021-03-03 23:30:32
464716   2021-03-03 23:49:10
Name: Etime, Length: 464717, dtype: datetime64[ns]
Duration=tmp1-tmp
Duration
taxiod['Duration']=Duration
taxiod
<ipython-input-10-8b258a85ed6d>:3: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  taxiod['Duration']=Duration
VehicleNumStimeSLngSLatELngELatEtimeDuration
02222300:03:23114.1674649999999922.562468114.2252350000000122.5527500:10:480 days 00:07:25
12222300:11:33114.2271522.554167114.2292179999999922.56021700:15:190 days 00:03:46
22222300:17:13114.2313540000000122.562166114.25579822.59096700000000300:29:060 days 00:11:53
32222300:36:45114.24019622.56365114.11996522.56666800:54:420 days 00:17:57
42222301:01:14114.1354139999999822.575933114.16674822.60826701:08:170 days 00:07:03
...........................
4647123694722:08:22113.91422.5314113.99722.545622:36:530 days 00:28:31
4647133694722:39:12114.00622.5481113.99622.537122:46:250 days 00:07:13
4647143694722:49:38113.99522.535113.92222.496523:13:150 days 00:23:37
4647153694723:24:24113.92122.5135113.9322.494223:30:320 days 00:06:08
4647163694723:37:09113.92822.5126113.91122.487923:49:100 days 00:12:01

464717 rows × 8 columns

taxiod.rename(columns={'duration': 'Duration'}, inplace=True)  # 重命名某列
C:\Program Files (x86)\Anaconda3\lib\site-packages\pandas\core\frame.py:4296: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  return super().rename(
taxiod
VehicleNumStimeSLngSLatELngELatEtimeDuration
02222300:03:23114.1674649999999922.562468114.2252350000000122.5527500:10:480 days 00:07:25
12222300:11:33114.2271522.554167114.2292179999999922.56021700:15:190 days 00:03:46
22222300:17:13114.2313540000000122.562166114.25579822.59096700000000300:29:060 days 00:11:53
32222300:36:45114.24019622.56365114.11996522.56666800:54:420 days 00:17:57
42222301:01:14114.1354139999999822.575933114.16674822.60826701:08:170 days 00:07:03
...........................
4647123694722:08:22113.91422.5314113.99722.545622:36:530 days 00:28:31
4647133694722:39:12114.00622.5481113.99622.537122:46:250 days 00:07:13
4647143694722:49:38113.99522.535113.92222.496523:13:150 days 00:23:37
4647153694723:24:24113.92122.5135113.9322.494223:30:320 days 00:06:08
4647163694723:37:09113.92822.5126113.91122.487923:49:100 days 00:12:01

464717 rows × 8 columns

r=taxiod['Duration'].iloc[0]
taxiod['order_time']=taxiod['Duration'].apply(lambda r:r.seconds)
<ipython-input-13-d23b5d7f6867>:2: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  taxiod['order_time']=taxiod['Duration'].apply(lambda r:r.seconds)
taxiod.drop(columns=['Duration'])
VehicleNumStimeSLngSLatELngELatEtimeorder_time
02222300:03:23114.1674649999999922.562468114.2252350000000122.5527500:10:48445
12222300:11:33114.2271522.554167114.2292179999999922.56021700:15:19226
22222300:17:13114.2313540000000122.562166114.25579822.59096700000000300:29:06713
32222300:36:45114.24019622.56365114.11996522.56666800:54:421077
42222301:01:14114.1354139999999822.575933114.16674822.60826701:08:17423
...........................
4647123694722:08:22113.91422.5314113.99722.545622:36:531711
4647133694722:39:12114.00622.5481113.99622.537122:46:25433
4647143694722:49:38113.99522.535113.92222.496523:13:151417
4647153694723:24:24113.92122.5135113.9322.494223:30:32368
4647163694723:37:09113.92822.5126113.91122.487923:49:10721

464717 rows × 8 columns

taxiod['hour']=taxiod['Stime'].apply(lambda r:r.split(':')[0])
taxiod
<ipython-input-15-c7c6b55b9ff2>:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  taxiod['hour']=taxiod['Stime'].apply(lambda r:r.split(':')[0])
VehicleNumStimeSLngSLatELngELatEtimeDurationorder_timehour
02222300:03:23114.1674649999999922.562468114.2252350000000122.5527500:10:480 days 00:07:2544500
12222300:11:33114.2271522.554167114.2292179999999922.56021700:15:190 days 00:03:4622600
22222300:17:13114.2313540000000122.562166114.25579822.59096700000000300:29:060 days 00:11:5371300
32222300:36:45114.24019622.56365114.11996522.56666800:54:420 days 00:17:57107700
42222301:01:14114.1354139999999822.575933114.16674822.60826701:08:170 days 00:07:0342301
.................................
4647123694722:08:22113.91422.5314113.99722.545622:36:530 days 00:28:31171122
4647133694722:39:12114.00622.5481113.99622.537122:46:250 days 00:07:1343322
4647143694722:49:38113.99522.535113.92222.496523:13:150 days 00:23:37141722
4647153694723:24:24113.92122.5135113.9322.494223:30:320 days 00:06:0836823
4647163694723:37:09113.92822.5126113.91122.487923:49:100 days 00:12:0172123

464717 rows × 10 columns

import matplotlib.pyplot as plt
fig  =plt.figure(1,(7,3),dpi=250)
ax   =plt.subplot(111)
plt.sca(ax)

plt.boxplot(taxiod['order_time']/60)
plt.ylabel('minutes')
plt.xlabel('order time')
plt.ylim(0,60)

plt.show()


在这里插入图片描述

import matplotlib.pyplot as plt
fig     = plt.figure(1,(10,5),dpi = 250)     
ax      = plt.subplot(111)
plt.sca(ax)

#整理数据
hour = taxiod['hour'].drop_duplicates().sort_values()
datas = []
for i in range(len(hour)):
    datas.append(taxiod[taxiod['hour']==hour.iloc[i]]['order_time']/60)
#绘制
plt.boxplot(datas)
#更改x轴ticks的文字
plt.xticks(range(1,len(hour)+1),list(hour))
###################################################################################

plt.ylabel('Order time(minutes)')
plt.xlabel('Order start time')
plt.ylim(0,60)


plt.show()


在这里插入图片描述

import seaborn as sns 
fig     = plt.figure(1,(10,5),dpi = 250)     
ax      = plt.subplot(111)
plt.sca(ax)

# 只需一行
sns.boxplot(x='hour',y=taxiod['order_time']/60,data=taxiod,ax=ax)

plt.ylabel('order_time(minutes)')
plt.xlabel('order start time')
plt.ylim(0,(60))
plt.show()


在这里插入图片描述


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值