当前值为0(零)时,复制上一行的值

时间:2018-11-04 10:11:43

标签: python python-3.x dataframe

我的数据框如下:

13664567    74.3    SELL    1581566.4   2378211.6   12:07:59
13665406    74.3    0       0           0           0
13665406    0       0       0           0           0
13665406    74.3    0       0           0           0
13667531    74.3    0       0           0           0
13667531    74.3    0       0           0           0
13672281    74.3    0       0           0           0
13672591    74.3    0       0           0           0
13672591    74.3    BUY     2388742.8   1590276     12:08:01
13681398    74.3    0       0           0           0
13681398    74.3    0       0           0           0
13684573    74.3    0       0           0           0
13685574    74.3    0       0           0           0
13685574    74.3    0       0           0           0
13688544    0       0       0           0           0
13689596    74.3    0       0           0           0
13689596    74.3    0       0           0           0
13705735    74.3    0       0           0           0
13706035    74.3    0       0           0           0
13712130    74.3    SELL    1583219.2   2408620.8   12:08:01
13712130    74.3    0       0           0           0
13715699    74.3    0       0           0           0
13720809    74.3    0       0           0           0
13726310    74.3    0       0           0           0
13726310    74.3    0       0           0           0
13726410    74.3    0       0           0           0

我想用先前的行值填充最后4列的0(零)值。

我想要如下所示的数据框:

13664567    74.3    SELL    1581566.4    2378212    12:07:59
13665406    74.3    SELL    1581566.4    2378212    12:07:59
13665406    0       SELL    1581566.4    2378212    12:07:59
13665406    74.3    SELL    1581566.4    2378212    12:07:59
13667531    74.3    SELL    1581566.4    2378212    12:07:59
13667531    74.3    SELL    1581566.4    2378212    12:07:59
13672281    74.3    SELL    1581566.4    2378212    12:07:59
13672591    74.3    SELL    1581566.4    2378212    12:07:59
13672591    74.3    BUY     2388742.8    1590276    12:08:01
13681398    74.3    BUY     2388742.8    1590276    12:08:01
13681398    74.3    BUY     2388742.8    1590276    12:08:01
13684573    74.3    BUY     2388742.8    1590276    12:08:01
13685574    74.3    BUY     2388742.8    1590276    12:08:01
13685574    74.3    BUY     2388742.8    1590276    12:08:01
13688544    0       BUY     2388742.8    1590276    12:08:01
13689596    74.3    BUY     2388742.8    1590276    12:08:01
13689596    74.3    BUY     2388742.8    1590276    12:08:01
13705735    74.3    BUY     2388742.8    1590276    12:08:01
13706035    74.3    BUY     2388742.8    1590276    12:08:01
13712130    74.3    SELL    1583219.2    2408621    12:08:01
13712130    74.3    SELL    1583219.2    2408621    12:08:01
13715699    74.3    SELL    1583219.2    2408621    12:08:01
13720809    74.3    SELL    1583219.2    2408621    12:08:01
13726310    74.3    SELL    1583219.2    2408621    12:08:01
13726310    74.3    SELL    1583219.2    2408621    12:08:01
13726410    74.3    SELL    1583219.2    2408621    12:08:01

我只想对最后四列进行更改,然后将上一行的值复制到下一行,直到新值可用。

请帮助。

2 个答案:

答案 0 :(得分:3)

使用update并用iloc选择最后4列,替换为缺失值并向前填充最近的先前值:

x.index(min(x[2:5]))

或使用df.update(df.iloc[:, -4:].replace([0,'0'], np.nan).ffill()) 替换mask

0

答案 1 :(得分:1)

您可以使用mask + ffill

values = df.iloc[:, -4:]
df.iloc[:, -4:] = values.mask(values.isin([0, '0'])).ffill()