现有奶茶店数据,有下单时间,开始制作奶茶时间,制作奶茶完成时间,一单中的杯量,门店编号,需要填充每个门店当天开始制作时间和制作完成时间的缺失值,写出Python代
时间: 2024-03-27 16:40:29 浏览: 18
以下是我编写的Python代码。需要使用Pandas和Numpy库。
```python
import pandas as pd
import numpy as np
# 读取数据
data = pd.read_csv('data.csv')
# 将下单时间转为datetime类型
data['order_time'] = pd.to_datetime(data['order_time'])
# 按门店编号和日期分组
groups = data.groupby(['store_id', data['order_time'].dt.date])
# 遍历每个分组
for name, group in groups:
# 获取开始制作时间和制作完成时间的缺失值所在的行
null_start = group['start_time'].isnull()
null_end = group['end_time'].isnull()
# 如果开始制作时间和制作完成时间都缺失,则跳过
if null_start.all() and null_end.all():
continue
# 获取已知的开始制作时间和制作完成时间
start_time = group['start_time'].dropna().values
end_time = group['end_time'].dropna().values
# 如果开始制作时间和制作完成时间都已知,则跳过
if len(start_time) == len(end_time) and len(start_time) == len(group):
continue
# 计算平均杯量
mean_cups = group['cups'].mean()
# 填充开始制作时间和制作完成时间的缺失值
if null_start.any():
start_time = np.concatenate(([group.iloc[0]['order_time']], end_time))
start_time = start_time[:-1] + (start_time[1:] - start_time[:-1]) / mean_cups * group['cups'].values[null_start].cumsum()
data.loc[null_start & (data['store_id'] == name[0]) & (data['order_time'].dt.date == name[1]), 'start_time'] = start_time[1:]
if null_end.any():
end_time = np.concatenate((start_time, [group.iloc[-1]['order_time']]))
end_time = end_time[1:] - (end_time[1:] - end_time[:-1]) / mean_cups * group['cups'].values[null_end][::-1].cumsum()[::-1]
data.loc[null_end & (data['store_id'] == name[0]) & (data['order_time'].dt.date == name[1]), 'end_time'] = end_time[:-1]
# 按门店编号和日期再次分组
groups = data.groupby(['store_id', data['order_time'].dt.date])
# 填充每个门店当天开始制作时间和制作完成时间的缺失值
for name, group in groups:
# 获取开始制作时间和制作完成时间的缺失值所在的行
null_start = group['start_time'].isnull()
null_end = group['end_time'].isnull()
# 如果开始制作时间和制作完成时间都缺失,则跳过
if null_start.all() and null_end.all():
continue
# 获取已知的开始制作时间和制作完成时间
start_time = group['start_time'].dropna().values
end_time = group['end_time'].dropna().values
# 如果开始制作时间和制作完成时间都已知,则跳过
if len(start_time) == len(end_time) and len(start_time) == len(group):
continue
# 计算平均杯量
mean_cups = group['cups'].mean()
# 填充开始制作时间和制作完成时间的缺失值
if null_start.any():
start_time = np.concatenate(([group.iloc[0]['order_time']], end_time))
start_time = start_time[:-1] + (start_time[1:] - start_time[:-1]) / mean_cups * group['cups'].values[null_start].cumsum()
data.loc[null_start & (data['store_id'] == name[0]) & (data['order_time'].dt.date == name[1]), 'start_time'] = start_time[1:]
if null_end.any():
end_time = np.concatenate((start_time, [group.iloc[-1]['order_time']]))
end_time = end_time[1:] - (end_time[1:] - end_time[:-1]) / mean_cups * group['cups'].values[null_end][::-1].cumsum()[::-1]
data.loc[null_end & (data['store_id'] == name[0]) & (data['order_time'].dt.date == name[1]), 'end_time'] = end_time[:-1]
# 输出结果
print(data)
```
这段代码与之前的代码基本相同,只是在遍历完每个分组后,又按门店编号和日期再次进行了分组,并填充了每个门店当天开始制作时间和制作完成时间的缺失值。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)