What's the best way to process values in a specific set of Dataframe columns only if it's not null?
my original code:
for i in columns_to_process:
df[i] = df[i].astype(str) + '!'
#columns_to_process is a list of selected df column name strings
I realised that this will turn null values into nan!
but I just want to keep them nulls.
I've looked into using .apply()
and a lambda
function but that's bad practice when working on individual columns apparently, according to: Pandas: How can I use the apply() function for a single column? . Seems like .apply()
is more for updating every column in the dataframe.
Then I came across this: replace non Null values in column by 1 and managed to make this working solution:
for i in columns_to_process:
df.loc[df[i].notnull(), i] = df.loc[df[i].notnull(), i].astype(str) + '!'
It took me a long time to figure this out and doesn't look very elegant, so I'm wondering if there is a better / more pythonic way to do this?