是否要根据旧列范围创建新列?

时间:2019-08-16 04:39:12

标签: python pandas dataframe

我要创建一个具有旧列日期范围的新列

df['block']= np.where((df['transacted_date']> '2016-06-01') & (df['transacted_date']< '2016-09-01') ,0,'None')
df['block']= np.where((df['transacted_date']> '2016-09-01') & (df['transacted_date']< '2016-12-01') ,1,'None')

如果使用elif语句,是否可以执行此操作?

2 个答案:

答案 0 :(得分:1)

尝试使用np.select

m1 = (df['transacted_date'] > '2016-06-01') & (df['transacted_date'] < '2016-09-01')


m2 = (df['transacted_date'] > '2016-09-01') &( df['transacted_date'] < '2016-12-01')

df['block'] = np.select(condlist=[m1,m2],
                        choicelist=[0,1],
                        default=None)

答案 1 :(得分:1)

numpy.selectSeries.between一起使用:

m1 = df['transacted_date'].between('2016-06-01', '2016-09-01', inclusive = False)
m2 = df['transacted_date'].between('2016-09-01', '2016-12-01', inclusive = False)

df['block'] = np.select([m1,m2], [0,1], default=None)

如果需要if-else解决方案:

def f(x): 
    if (x > pd.Timestamp('2016-06-01')) and (x < pd.Timestamp('2016-09-01')): 
        return 0
    elif (x > pd.Timestamp('2016-09-01')) and (x < pd.Timestamp('2016-12-01')): 
        return 1
    else: 
        return None

df['block']=df['transacted_date'].apply(f)

如果需要更一般的解决方案,请将cutnumpy.where一起使用,因为cut无法创建NoneNaN标签:

b = pd.to_datetime([pd.Timestamp.min,'2016-06-01','2016-09-01','2016-12-01',pd.Timestamp.max])

s = pd.cut(df['transacted_date'], bins=b, labels=[-2, 0, 1, -1])
df['block1'] = np.where(s.astype(int) >= 0, s, np.nan)