I want to change DateWork['Variable'] values as per multiple where conditions and update in DateWork['Date']
If Frequency=3 and len(Variable)=6 then replace M with "-0" and update in DateWork['Date']
If Frequency=3 and len(Variable)=7 then replace M with "-" and update in DateWork['Date']
DataFrame: DateWork
Frequency Variable Date
3 1950M2 1950-02-01
3 1950M3 1950-03-01
2 1950-07-01 1950-07-01
3 1950M9 1950-09-01
2 1950-10-01 1950-10-01
3 1950M10 1950-10-01
My code:
DateWork.loc[DateWork['Date']] = np.where(((DateWork['Frequency'] == 3) & (DateWork['variable'].str.len() == 6)), 'M', '-0', DateWork['Date'])
DateWork.loc[DateWork['Date']] = np.where(((DateWork['Frequency'] == 3) & (DateWork['variable'].str.len() == 7)), 'M', '-', DateWork['Date'])
DateWork.loc[DateWork['Frequency'] == 3, 'Date'] = DateWork.loc[DateWork['Frequency'] == 3, 'variable'] + '-01'
This gives error:
TypeError: where() takes at most 3 arguments (4 given)