I have a df which looks like this:
JavaScript
x
4
1
df = pd.DataFrame({'Date':['2019-09-23','2019-09-24','2019-09-25','2019-09-26','2019-09-27','2019-09-28','2019-09-29'],
2
'Sep':[1,10,5,'NaN','NaN','NaN','NaN'],
3
'Dec':[2,8,4,7,9,1,5]})
4
I’m trying to create a new column called ‘First_Contract’:
- ‘First_Contract’ needs to take the third-last value of ‘Sep’ column, before ‘Sep’column reaches NaN.
- The subsequent values need to be filled with ‘Dec’ column values.
Desired output:
JavaScript
1
5
1
df2= pd.DataFrame({'Date':['2019-09-23','2019-09-24','2019-09-25','2019-09-26','2019-09-27','2019-09-28','2019-09-29'],
2
'Sep':[1,10,5,'NaN','NaN','NaN','NaN'],
3
'Dec':[2,8,4,7,9,1,5],
4
'First_Contract':[1,8,4,7,9,1,5]})
5
How do I go about to achieve this?
Advertisement
Answer
Let us do it step by step
JavaScript
1
15
15
1
df.Sep.replace({'NaN': np.nan}, inplace=True)
2
df['FC'] = df['Dec']
3
ids = df.Sep.last_valid_index()-2
4
df.loc[ids,'FC'] = df.Sep[ids]
5
df
6
Out[126]:
7
Date Sep Dec First_Contract FC
8
0 2019-09-23 1.0 2 1 1.0
9
1 2019-09-24 10.0 8 8 8.0
10
2 2019-09-25 5.0 4 4 4.0
11
3 2019-09-26 NaN 7 7 7.0
12
4 2019-09-27 NaN 9 9 9.0
13
5 2019-09-28 NaN 1 1 1.0
14
6 2019-09-29 NaN 5 5 5.0
15