简体   繁体   English

熊猫:根据条件在两者之间进行填充

[英]Pandas: Fill forward between based on a condition

I want to fill values between two number from two different column. 我想在两个不同列的两个数字之间填充值。 I have a data frame that looks like this(df) I only want to fill the forward the Value column when I have a number in Start column and End column . 我有一个看起来像this(df)的数据框,当我在开始列结束列中有数字时,我只想填充正向 Basically, the Start indicates that a process is getting started and end meaning that process is ending. 基本上,“开始”表示一个进程正在开始,而“结束”表示该进程正在结束。 So Value column only can have to be filled between these two. 因此,仅必须在这两者之间填写“ 值”列

Here is a code to generate the data and DF is what I want to get. 这是生成数据的代码,DF是我想要的。

from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import random
np.random.seed(11) 
date_today = datetime.now()
ndays = 10
df = pd.DataFrame({'date': [date_today + timedelta(days=x) for x in range(ndays)], 
               'Start': pd.Series(np.random.randn(ndays)),     'End':pd.Series(np.random.randn(ndays))})
 df = df.set_index('date')
 df = df.mask(np.random.random(df.shape) < .6)
 df.End[{0,1,2,5,6,9}]=np.nan
 df.Start[5]=1
 df.Start[{1,3,4,2,8, 9}]=np.nan
 df['Value']=np.nan
 df.Value[{0,5}]=[0.3,0.1]
 df

I want to obtain a dataframe(DF) which look like this: 我想获得一个像这样的dataframe(DF):

                                 End     Start  Value
date                                                 
2018-06-18 22:34:35.964286       NaN  1.749455    0.3
2018-06-19 22:34:35.964286       NaN       NaN    0.3
2018-06-20 22:34:35.964286       NaN       NaN    0.3
2018-06-21 22:34:35.964286  0.561192       NaN    0.3
2018-06-22 22:34:35.964286       NaN       NaN    NaN
2018-06-23 22:34:35.964286       NaN  1.000000    0.1
2018-06-24 22:34:35.964286       NaN       NaN    0.1
2018-06-25 22:34:35.964286       NaN       NaN    0.1
2018-06-26 22:34:35.964286 -0.031075       NaN    0.1
2018-06-27 22:34:35.964286       NaN       NaN    NaN

Thanks in advance 提前致谢

Try this: First you forward fill. 尝试以下操作:首先,您要向前填充。 Then calculate the number of 'events'. 然后计算“事件”的数量。 Then replace values with NaN if the number of 'events' is even. 然后,如果“事件”的数量为偶数,则用NaN替换值。

df['Value'] = df['Value'].fillna(method='ffill')
temp = (df['End'].shift().notnull().astype(int) + df['Start'].notnull().astype(int)).cumsum()
df.loc[temp % 2 == 0, 'Value'] = np.nan

Edit: we have to use shift() to modify 'End' because otherwise it will count the 'End' events on the same row that they occur. 编辑:我们必须使用shift()来修改'End',因为否则它将在发生它们的同一行中对'End'事件进行计数。 We want the 'End' events to be counted just after they occur so that 'Value' is not voided on the 'End' row. 我们希望在“结束”事件发生后立即对其进行计数,以免“结束”行上的“值”无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM