这是一个简单的数据框
import pandas as pd
import numpy as np
dates = pd. date_range(' 20130101' , periods=14)
data = pd.DataFrame({'a':[1,0,0,1,0,0,0,1,1,0,0,1,0,0],'b':[0,0,1,0,0,1,0,0,0,0,1,0,1,0]},index=dates)
现在我想在以下条件下添加列' c'
if a = 1, c = 1
if b = 1, c = 0
if a = 0 and b = 0, c = c.shift(1)
约束:不存在同时a = 1
和b = 1
的情况。这是一个简单的问题,但很难解决......
有什么好主意吗?
答案 0 :(得分:2)
你需要的IIUC:
data['c'] = np.where(data.a == 1, 1,
np.where(data.b == 1, 0, np.nan))
print (data)
a b c
2013-01-01 1 0 1.0
2013-01-02 0 0 NaN
2013-01-03 0 1 0.0
2013-01-04 1 0 1.0
2013-01-05 0 0 NaN
2013-01-06 0 1 0.0
2013-01-07 0 0 NaN
2013-01-08 1 0 1.0
2013-01-09 1 0 1.0
2013-01-10 0 0 NaN
2013-01-11 0 1 0.0
2013-01-12 1 0 1.0
2013-01-13 0 1 0.0
2013-01-14 0 0 NaN
然后我不确定是否需要bfill
或ffill
:
data['c'] = data['c'].bfill()
print (data)
a b c
2013-01-01 1 0 1.0
2013-01-02 0 0 0.0
2013-01-03 0 1 0.0
2013-01-04 1 0 1.0
2013-01-05 0 0 0.0
2013-01-06 0 1 0.0
2013-01-07 0 0 1.0
2013-01-08 1 0 1.0
2013-01-09 1 0 1.0
2013-01-10 0 0 0.0
2013-01-11 0 1 0.0
2013-01-12 1 0 1.0
2013-01-13 0 1 0.0
2013-01-14 0 0 NaN
data['c'] = data['c'].ffill()
print (data)
a b c
2013-01-01 1 0 1.0
2013-01-02 0 0 1.0
2013-01-03 0 1 0.0
2013-01-04 1 0 1.0
2013-01-05 0 0 1.0
2013-01-06 0 1 0.0
2013-01-07 0 0 0.0
2013-01-08 1 0 1.0
2013-01-09 1 0 1.0
2013-01-10 0 0 1.0
2013-01-11 0 1 0.0
2013-01-12 1 0 1.0
2013-01-13 0 1 0.0
2013-01-14 0 0 0.0
答案 1 :(得分:2)
替代
data.assign(
c=np.where(v.sum(1, keepdims=1), (np.diff(v[:, ::-1]) + 1) / 2, np.nan)
).ffill()
a b c
2013-01-01 1 0 1.0
2013-01-02 0 0 1.0
2013-01-03 0 1 0.0
2013-01-04 1 0 1.0
2013-01-05 0 0 1.0
2013-01-06 0 1 0.0
2013-01-07 0 0 0.0
2013-01-08 1 0 1.0
2013-01-09 1 0 1.0
2013-01-10 0 0 1.0
2013-01-11 0 1 0.0
2013-01-12 1 0 1.0
2013-01-13 0 1 0.0
2013-01-14 0 0 0.0