Issue
I have the following sample data:
date | value | |
---|---|---|
0 | 2021/05 | 50 |
1 | 2021/06 | 60 |
2 | 2021/07 | 70 |
3 | 2021/08 | 80 |
4 | 2021/09 | 90 |
5 | 2021/10 | 100 |
I want to update the data in the 'date' column, where for example '2021/05' becomes '05/10/2021', '2021/06' becomes '06/12/2021' and so long (I have to choose the new date manually for every row).
Is there a better/more clever way to do it instead of:
for i in df.index:
if df['date'][i] == '2021/05':
df['date'][i] = '05/10/2021'
elif df['date'][i] == '2021/06':
df['date'][i] = '06/12/2021'
The problem is that there are more than hundred rows that have to be updated and the code above will be tremendously long.
Solution
We can use the select
method from numpy
like so :
import numpy as np
condlist = [df['date'] == '2021/05',
df['date'] == '2021/06']
choicelist = ['05/10/2021',
'06/12/2021']
df['date'] = np.select(condlist, choicelist, default=np.nan)
Answered By - tlentali
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.