如何使用python有效地填充“缺失时间模式”和“以特定值填充”?

时间:2019-02-15 17:56:50

标签: python pandas bigdata

我想从以下位置“扩展”我的行:

+-------------+---------+-------+-------+
| Week Number | Weekday | Time  | Speed |
+-------------+---------+-------+-------+
|           1 | Monday  | 09.00 |     2 |
|           1 | Monday  | 12.00 |     2 |
|           1 | Monday  | 14.00 |     2 |
|           1 | Monday  | 15.00 |     1 |
|           1 | Tuesday | 08.00 |     4 |
|           1 | Tuesday | 10.00 |     2 |
|           1 | Tuesday | 11.00 |     3 |
|           1 | Tuesday | 13.00 |     2 |
+-------------+---------+-------+-------+

每天更改为以下格式: 08.00,09.00,10.00,11.00,12.00,13.00,14.00,15.00

+-------------+---------+-------+-------+
| Week Number | Weekday | Time  | Speed |
+-------------+---------+-------+-------+
|           1 | Monday  | 08.00 |     0 |
|           1 | Monday  | 09.00 |     2 |
|           1 | Monday  | 10.00 |     0 |
|           1 | Monday  | 11.00 |     0 |
|           1 | Monday  | 12.00 |     2 |
|           1 | Monday  | 13.00 |     0 |
|           1 | Monday  | 14.00 |     2 |
|           1 | Monday  | 15.00 |     1 |
|           1 | Tuesday | 08.00 |     4 |
|           1 | Tuesday | 09.00 |     0 |
|           1 | Tuesday | 10.00 |     2 |
|           1 | Tuesday | 11.00 |     3 |
|           1 | Tuesday | 12.00 |     0 |
|           1 | Tuesday | 13.00 |     3 |
|           1 | Tuesday | 14.00 |     0 |
|           1 | Tuesday | 15.00 |     0 |
+-------------+---------+-------+-------+

,并用0填充“缺失”。 我应该怎么办?

我正在将python 3.6和pandas库一起使用。

1 个答案:

答案 0 :(得分:0)

import pandas as pd
df = pd.DataFrame({'Week Number': 1, 'Weekday': ['Monday'] * 4 + ['Tuesday'] * 4, 'Time':['09.00', '12.00', '14.00', '15.00'] * 2,
                  'Speed': [2, 4] * 4})

假设timesdaysweek_nums都是要扩展DataFrame的值

times = ['08.00', '09.00', '10.00', '11.00', '12.00', '13.00', '14.00', '15.00']
days = ['Monday', 'Tuesday']
week_nums = [1]

使用Speed = 0

创建所有可能组合的DataFrame
from itertools import product
df_combinations = pd.DataFrame(list(product(, days, times, [0])), columns=['Week Number', 'Weekday', 'Time', 'Speed'])

连接两个数据帧(df_combinations必须是第二个重复删除对象!)

df_new = pd.concat([df, df_combinations], ignore_index=True, sort=False)

创建重复项的二进制掩码,将其删除并对数据框进行排序

df_new = df_new[~df_new.duplicated(subset=['Week Number', 'Weekday', 'Time'], keep='first')]
df_new.sort_values(['Week Number', 'Weekday', 'Time'])