简体   繁体   中英

I don't understand the difference between these two lines of code and why they return different results

I don't understand the difference between these two lines of code, and why they return different results when I use brackets.

df["Winner fav"] = (df["WPts"] - df["LPts"]).apply(lambda x: 1 if x>0 else 0)

df2["Winner fav"] = df2["WPts"] - df2["LPts"].apply(lambda x: 1 if x>0 else 0)

It's an operator precedence issue. The first one,

df["Winner fav"] = (df["WPts"] - df["LPts"]).apply(lambda x: 1 if x>0 else 0)

applies the lambda function on the difference of df["WPts"] - df["LPts"] .

The second one,

df2["Winner fav"] = df2["WPts"] - df2["LPts"].apply(lambda x: 1 if x>0 else 0)

first applies the lambda on the value of df2["LPts"] and then subtracts 1 or 0 from df2["WPts"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM