使用下面的测试Pandas数据帧,我试图在匹配某些条件后从另一个数据帧中的匹配索引复制一个值。
这是来自名为data2
的数据框的剪辑:
Signal Value2
2013-01-01 09:00:00 1.0 NaN
2013-01-01 10:00:00 1.0 NaN
2013-01-01 11:00:00 1.0 NaN
2013-01-01 12:00:00 1.0 NaN
2013-01-01 13:00:00 1.0 NaN
2013-01-01 14:00:00 -1.0 NaN
这是来自data
的剪辑:
value
2013-01-01 09:00:00 9
2013-01-01 10:00:00 10
2013-01-01 11:00:00 11
2013-01-01 12:00:00 12
2013-01-01 13:00:00 13
2013-01-01 14:00:00 14
2013-01-01 15:00:00 15
2013-01-01 16:00:00 16
2013-01-02 09:00:00 33
2013-01-02 10:00:00 34
当data2
Signal
2013-01-01 14:00:00
显示-1
时,我想从value
data
复制相应的14
data2
并将其复制到Values2
import pandas as pd
import datetime
import numpy as np
index = pd.date_range('2013-1-1',periods=100,freq='1h')
data = pd.DataFrame(data=list(range(100)), columns=['value'], index=index)
signal = 1.0
data2 = pd.DataFrame(data=signal, columns=['Signal'], index=index)
data2['Signal']['2013-01-01 14:00:00'] = -1.0
data2['Value2'] = np.nan
start = datetime.time(9,0,0)
end = datetime.time(16,00,0)
data = data.between_time(start,end)
。
以下是测试此代码的代码:
$(this).closest('tr').next().find('.lastName-holder').html('test');
这将最终用于大型数据框,并将涉及多天。
答案 0 :(得分:3)
可能是这样的吗?
data2.loc[data2.Signal == -1, 'Value2'] = data.loc[data2.Signal == -1, 'value']