python - 根据条件在 Pandas 数据框行之间填充多行

标签 python pandas

我有一个数据框,其中有日期明智的提款/贷记和期末余额信息。

date      withdraw     credit    closing_balance
02/06/17    2,500.00      nan           6,396.77
03/06/17      nan      36,767.00       43,163.77
05/06/17    1,770.00      nan          41,393.77
05/06/17     6000.00      nan           35393.77
05/06/17      278.00      nan           35115.77
07/06/17     1812.00      nan           33303.77

现在我们可以看到此表中缺少 2 天的条目。 即 04/06/17 和 06/06/17。由于当天没有交易。

我要做的是在数据框中为这些日期(第 4 天和第 6 天)添加虚拟行

提款栏为0,信用栏为0

并且期末余额列与前一天的最后一个期末余额条目相同。

预期输出

date      withdraw     credit    closing_balance
02/06/17    2,500.00      nan           6,396.77
03/06/17      nan      36,767.00       43,163.77
04/06/17     nan(or 0)  nan(or 0)      43,163.77
05/06/17    1,770.00      nan          41,393.77
05/06/17     6000.00      nan           35393.77
05/06/17      278.00      nan           35115.77
06/06/17    nan(or 0)   nan(or 0)       35115.77
07/06/17     1812.00      nan           33303.77

有没有一种 pythonic 的方式来做到这一点。

我的想法是首先找到缺失的日期,然后为这些日期创建一个临时数据框,然后将其与主数据框连接起来,然后进行排序。

但我在如何获取前几天的最后收盘余额条目以填补缺失的天数收盘余额方面遇到了问题。

最佳答案

想法是用 merge 添加所有缺失的日期时间并由另一个使用最小和最大日期时间和 date_range 创建的 DataFrame 加入。 .然后向前填充 closing_balance 的缺失值并为新的日期时间设置 0:

df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%y')

df1 = pd.DataFrame({'Date':pd.date_range(df['Date'].min(), df['Date'].max())})

df2 = df1.merge(df, how='left')
df2['closing_balance'] = df2['closing_balance'].ffill()
df2.loc[~df2['Date'].isin(df['Date']), ['withdraw','credit']] = 0
print (df2)
        Date  withdraw     credit closing_balance
0 2017-06-02  2,500.00        NaN        6,396.77
1 2017-06-03       NaN  36,767.00       43,163.77
2 2017-06-04         0          0       43,163.77
3 2017-06-05  1,770.00        NaN       41,393.77
4 2017-06-05   6000.00        NaN        35393.77
5 2017-06-05    278.00        NaN        35115.77
6 2017-06-06         0          0        35115.77
7 2017-06-07   1812.00        NaN        33303.77

使用 mergeindicator 参数设置 0 值的类似想法:

df['Date'] = pd.to_datetime(df['Date'], format='%d/%m/%y')

df1 = pd.DataFrame({'Date':pd.date_range(df['Date'].min(), df['Date'].max())})

df2 = df1.merge(df, how='left', indicator=True)
df2['closing_balance'] = df2['closing_balance'].ffill()
df2.loc[df2.pop('_merge').eq('left_only'), ['withdraw','credit']] = 0
print (df2)
        Date  withdraw     credit closing_balance
0 2017-06-02  2,500.00        NaN        6,396.77
1 2017-06-03       NaN  36,767.00       43,163.77
2 2017-06-04         0          0       43,163.77
3 2017-06-05  1,770.00        NaN       41,393.77
4 2017-06-05   6000.00        NaN        35393.77
5 2017-06-05    278.00        NaN        35115.77
6 2017-06-06         0          0        35115.77
7 2017-06-07   1812.00        NaN        33303.77

关于python - 根据条件在 Pandas 数据框行之间填充多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57841365/

相关文章:

python - Pandas groupby 和传递性属性

python - 当有多个变量时使用lambda在python数据帧中实现if-else

Treebank 的 Python 数据结构?

python - Django 查询特殊字符

python - BeautifulSoup : Parsing only one element

python - django AbstractUser模型 'str'对象没有属性 '_meta'

python - 一次制作多个地 block

python - Pandas:跨与列值关联的动态窗口大小的计算

python - 当数据点为 float 据类型时,如何将列与最近的数据点合并?

python - 我正在寻找一种在 python 中生成代码的简单方法