Home > Back-end >  Edit entire column in dataframe based on value in last row of that column
Edit entire column in dataframe based on value in last row of that column

Time:01-18

I have a dataframe, and I want to set the entire value of the column to 0 if the value in the last row of that column is <0. The current code i have is:

ccy.loc[(ccy['CumProfit'].iloc[-1] < 0), 'CumProfit'] = 0

CodePudding user response:

Use if:

if (ccy['CumProfit'].iloc[-1] < 0):
    ccy['CumProfit'] = 0

Or if-else for one line solution:

ccy['CumProfit'] = ccy['CumProfit'] if ccy['CumProfit'].iloc[-1] < 0 else 0
  •  Tags:  
  • Related