I'm trying to do a simple operation, converting a dataframe to titlecase.
There are some NaNs which cause errors, so I'd like to avoid them by applying str.title() only if it's not null.
However I'm getting invalid syntax.
df= df.applymap(lambda x: x.title() if pd.notnull(x))
^
SyntaxError: invalid syntax
Another try:
df= df.applymap(lambda x: x.title() if not pd.isnull(x))
SyntaxError: invalid syntax
this is a conditional expression you have to also provide a value for the else (if the condition is not met).
try this:
df= df.applymap(lambda x: x.title() if pd.notnull(x) else '')
pandas.Series.str.titledf.stack().str.title().unstack()
numpy.core.defchararray.titlefrom numpy.core.defchararray import title
title(df.to_numpy().astype(str))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With