使用 apply 和 map 在 Pandas 中添加两列的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41832925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:51:33  来源:igfitidea点击:

add the values of two columns in pandas using apply and map

pythonpandas

提问by Shiva Krishna Bavandla

Basically i was trying to add two columns data and reflect it on another column and the data was below

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

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

I am using above data frame for testing and I want to add the data of two columns coverageand reportsand replace it with one column as below

我使用的数据帧以上的测试,我想补充两列的数据coverage,并reports与一列如下更换

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

and my result should be

我的结果应该是

        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

But it was not working, can anyone please let me know what's wrong in the above code ?

但它不起作用,任何人都可以让我知道上面的代码有什么问题吗?

Edit

编辑

My add function

我的添加功能

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

回答by jezrael

You need simply add:

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

Or:

或者:

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

Your code need axis=1:

您的代码需要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