使用apply和map

时间:2017-01-24 16:09:35

标签: python pandas pandas-map pandas-apply

基本上我试图添加两列数据并将其反映在另一列上,数据位于

之下
data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
        'year': [2012, 2012, 2013, 2014, 2014], 
        'reports': [4, 24, 31, 2, 3],
        'coverage': [25, 94, 57, 62, 70]}
df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
df


        coverage    name    reports year
Cochice    25      Jason      4    2012
Pima       94      Molly     24    2012
Santa Cruz 57      Tina      31    2013
Maricopa   62      Jake      2     2014
Yuma       70       Amy      3     2014

我使用上面的数据框进行测试,我想添加两列coveragereports的数据,并将其替换为一列,如下所示

df[["coverage"]] = df[["coverage"]].apply(lambda value:add(df["coverage"], df["reports"]))

我的结果应该是

        coverage       name reports year
Cochice    25+4=29     Jason      4    2012
Pima       94+24       Molly     24    2012
Santa Cruz 57+31       Tina      31    2013
Maricopa   62+2        Jake      2     2014
Yuma       70+3        Amy       3     2014

但它没有用,有谁能告诉我上面的代码有什么问题?

修改

我的添加功能

def add(one, two):
  return one + two

1 个答案:

答案 0 :(得分:5)

您只需要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