mardi 26 février 2019

Apply 'if' condition to compare two pandas column and form a third column with the value of the second column

I have two columns in pandas dataframe and want to compare their values against each other and return a third column processing a simple formula.

  if post_df['pos'] == 1:

    if post_df['lastPrice'] < post_df['exp']:
        post_df['profP'] = post_df['lastPrice'] - post_df['ltP']
        post_df['pos'] = 0
    else:
        post_df['profP'] = post_df['lastPrice'] - post_df['ltP']

However, when I run the above code I get the following error: if post_df['pos'] == 1: File "/Users/srikanthiyer/Environments/emacs/lib/python3.7/site-packages/pandas/core/generic.py", line 1479, in __nonzero__ .format(self.__class__.__name__)) ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I have tried using np.where which works but since I intend to build a complex conditional structure want to keep it simple using if statements.

Aucun commentaire:

Enregistrer un commentaire