python-3.x - 根据特定条件生成带有填充的行 - pandas

标签 python-3.x pandas dataframe

我有一个 df 如下所示

Date                   B_best         B_worst     W_best            W_worst
2020-08-11             3              1           10                7
2020-08-12             3              1           10                7
2020-08-13             3              1           10                7
2020-08-14             3              1           10                7
2020-08-15             6              1           10                7
2020-08-16             6              2           10                11
2020-08-17             6              2           5                 11
2020-08-18             6              2           5                 11
2020-08-19             9              2           5                 11
2020-08-20             9              2           4                 11
2020-08-21             9              2           4                 11   
2020-08-22             5              3           7                 13         
2020-08-23             10             6           8                 16 
2020-08-24             9              2           4                 11

上面的 df 有一个名为 Date 的列,我想要一个函数,其输入将位于 df 之上和日期值

df1 = generate_data(df, datetime.datetime(2020, 9, 2, 20, 0))

其中 datetime.datetime(2020, 9, 2, 20, 0) 表示 2020-09-02

预期输出

df1:

Date                   B_best         B_worst     W_best            W_worst
2020-08-11             3              1           10                7
2020-08-12             3              1           10                7
2020-08-13             3              1           10                7
2020-08-14             3              1           10                7
2020-08-15             6              1           10                7
2020-08-16             6              2           10                11
2020-08-17             6              2           5                 11
2020-08-18             6              2           5                 11
2020-08-19             9              2           5                 11
2020-08-20             9              2           4                 11
2020-08-21             9              2           4                 11   
2020-08-22             5              3           7                 13         
2020-08-23             10             6           8                 16 
2020-08-24             9              2           4                 11
2020-08-25             9              2           4                 11
2020-08-26             9              2           4                 11
2020-08-27             9              2           4                 11
2020-08-28             9              2           4                 11
2020-08-29             9              2           4                 11
2020-08-30             9              2           4                 11
2020-08-31             9              2           4                 11
2020-09-01             9              2           4                 11
2020-09-02             9              2           4                 11

它已生成截至 2020 年 9 月 2 日的数据,其值与可用 df 的最后一行相同。

注意:

如果输入日期大于最大日期则返回 df1,否则返回 df

最佳答案

假设日期是连续的,并且输入日期始终大于数据框中可用的最小日期,您可以使用 min 创建一个 date_range 系列从现有的Date列,然后 right合并后填充:

def generate_data(a,b):
    idx = pd.date_range(a['Date'].min(),b)
    return a.merge(pd.Series(idx,name='Date'),how='right').ffill().astype(a.dtypes)
print(generate_data(df, datetime.datetime(2020, 9, 2, 20, 0)))

编辑1: 如果输入日期小于最小日期,则调整并返回原始 df,您可以编辑函数:

def generate_data(a,b):
    idx = pd.date_range(a['Date'].min(),b)
    out = a.merge(pd.Series(idx,name='Date'),how='right').ffill().astype(a.dtypes)
    return a if b<a['Date'].min() else out

编辑2:

如果输入日期大于最大日期,则返回 df1,否则返回 df。

def generate_data(df, b):
    if df['Date'].max() <  b:
        idx = pd.date_range(df['Date'].min(),b)
        return df.merge(pd.Series(idx,name='Date'),how='right').ffill().astype(df.dtypes)
    else:
        return df

         Date  B_best  B_worst  W_best  W_worst
0  2020-08-11       3        1      10        7
1  2020-08-12       3        1      10        7
2  2020-08-13       3        1      10        7
3  2020-08-14       3        1      10        7
4  2020-08-15       6        1      10        7
5  2020-08-16       6        2      10       11
6  2020-08-17       6        2       5       11
7  2020-08-18       6        2       5       11
8  2020-08-19       9        2       5       11
9  2020-08-20       9        2       4       11
10 2020-08-21       9        2       4       11
11 2020-08-22       5        3       7       13
12 2020-08-23      10        6       8       16
13 2020-08-24       9        2       4       11
14 2020-08-25       9        2       4       11
15 2020-08-26       9        2       4       11
16 2020-08-27       9        2       4       11
17 2020-08-28       9        2       4       11
18 2020-08-29       9        2       4       11
19 2020-08-30       9        2       4       11
20 2020-08-31       9        2       4       11
21 2020-09-01       9        2       4       11
22 2020-09-02       9        2       4       11

关于python-3.x - 根据特定条件生成带有填充的行 - pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63564180/

相关文章:

python - 替换 Pandas 数据框中的特殊字符

python - Pandas 数据框组内的计算

python - 嵌套列表索引

python - 使用 lxml 覆盖 XML 中的文本

python - 为什么截断 BytesIO 会把它搞砸?

python - pandas:如何为列中的每个子字符串复制一个值

python - pandas stack and unstack performance reduced after dataframe compression 并且比 R 的 data.table 差很多

python - 通过添加列来更新 NumPy 数组

python - Pandas:根据重复索引值加速 df.loc

python - 如何从数据框中删除特定列