I have a pandas dataframe with many columns of which some are numerical and other categorical.
I want to calculate the number of negative entries in the pandas dataframe. One way is to find which columns are numeric, subset these columns and then use simple syntax to calculate number of entries with negative values, e.g. (df < 0).sum()
Instead I tried a syntax with apply and lambda function which includes a conditional but I get a message that my syntax is erroneous. Could you please explain to me why and how this idea could be implemented?
data.apply(lambda x: (if (x.dtype == 'int16' or x.dtype == 'float16'): (x<0).sum())).sum()
File "<ipython-input-75-f329bf4e8cdd>", line 1
data.apply(lambda x: (if (x.dtype == 'int16' or x.dtype == 'float16'): (x<0).sum())).sum()
^
SyntaxError: invalid syntax
Aucun commentaire:
Enregistrer un commentaire