I want to create new column name 'pred_date' in df and set value according to specific index.
For example for index [1, 2, 3] I want to set value as the value in date column.
df:
date label
1 1.1 1
2 2.1 0
3 3.1 1
4 4.1 1
new df:
date label pred_date
1 1.1 1 1.1
2 2.1 0 2.1
3 3.1 1 3.1
4 4.1 1 Nan
>Solution :
You could use index.isin + where:
df['pred_date'] = df['date'].where(df.index.isin([1,2,3]))
Output:
date label pred_date
1 1.1 1 1.1
2 2.1 0 2.1
3 3.1 1 3.1
4 4.1 1 NaN