你只需要add
:
df["coverage"] = df["coverage"].add(df["reports"]) print (df) coverage name reports year Cochice 29 Jason 4 2012 Pima 118 Molly 24 2012 Santa Cruz 88 Tina 31 2013 Maricopa 64 Jake 2 2014 Yuma 73 Amy 3 2014
要么:
df["coverage"] = df["coverage"] + df["reports"] print (df) coverage name reports year Cochice 29 Jason 4 2012 Pima 118 Molly 24 2012 Santa Cruz 88 Tina 31 2013 Maricopa 64 Jake 2 2014 Yuma 73 Amy 3 2014
您的代码需要axis=1
:
def add(one, two): return one + two df["coverage"] = df.apply(lambda x :add(x["coverage"], x["reports"]), axis=1) print (df) coverage name reports year Cochice 29 Jason 4 2012 Pima 118 Molly 24 2012 Santa Cruz 88 Tina 31 2013 Maricopa 64 Jake 2 2014 Yuma 73 Amy 3 2014