This is a follow up on this question: How to create new column based on substrings in other column in a pandas dataframe?
The dataframe has the following structure
df = pd.DataFrame({
'Other input': ['Text A', 'Text B', 'Text C', 'Text D', 'Text E'],
'Substance': ['(NPK) 20/10/6', NaN, '46%N / O%P2O5 (Urea)', '46%N / O%P2O5 (Urea)', '(NPK) DAP Diammonphosphat; 18/46/0'],
'value': [0.2, NaN, 0.6, 0.8, .9]
})
Other Input substance value
0 Text A (NPK) 20/10/6 0.2
1 Text B NaN NaN
2 Text C 46%N / O%P2O5 (Urea) 0.6
3 Text D 46%N / O%P2O5 (Urea) 0.8
4 Text E (NPK) DAP Diammonphosphat; 18/46/0 0.9
It was created by merging two df's with a left join and it turns out that I have rows without substance and value. I need to replace the substance with a Short Name and before having missing values in the dataset, the following code worked:
test['Short Name'] = test['Substance'].apply(lambda x: 'Urea' if 'Urea' in x else 'DAP' if 'DAP' in x else '(NPK)')
How can I make this work with NaN (or 0 if that is easier)? Is there something equivalent to na_action=None that apparently works with applymap?