通过添加其他列的值,在Panda数据框中创建新列

时间:2022-05-21 20:22:32

I have a dataframe with values like

我有一个数据框,其值为

A B
1 4
2 6
3 9

I need to add a new column by adding values from column A and B, like

我需要通过添加A列和B列的值来添加新列,例如

A B C
1 4 5
2 6 8
3 9 12

I believe this can be done using lambda function, but I can't figure out how to do it.

我相信这可以使用lambda函数完成,但我无法弄清楚如何做到这一点。

6 个解决方案

#1


33  

Very simple:

df['C'] = df['A'] + df['B']

#2


25  

The simplest way would be to use DeepSpace answer. However, if you really want to use an anonymous function you can use apply:

最简单的方法是使用DeepSpace答案。但是,如果您真的想使用匿名函数,可以使用apply:

df['C'] = df.apply(lambda row: row['A'] + row['B'], axis=1)

#3


14  

You could use sum function to achieve that as @EdChum mentioned in the comment:

您可以使用sum函数来实现注释中提到的@EdChum:

df['C'] =  df[['A', 'B']].sum(axis=1)

In [245]: df
Out[245]: 
   A  B   C
0  1  4   5
1  2  6   8
2  3  9  12

#4


10  

Building a little more on Anton's answer, you can add all the columns like this:

在Anton的答案上建立更多内容,您可以添加如下所有列:

df['sum'] = df[list(df.columns)].sum(axis=1)

#5


4  

As of Pandas version 0.16.0 you can use assign as follows:

从Pandas版本0.16.0开始,您可以使用assign如下:

df = pd.DataFrame({"A": [1,2,3], "B": [4,6,9]})
df.assign(C = df.A + df.B)

# Out[383]: 
#    A  B   C
# 0  1  4   5
# 1  2  6   8
# 2  3  9  12

You can add multiple columns this way as follows:

您可以通过以下方式添加多个列:

df.assign(C = df.A + df.B,
          Diff = df.B - df.A,
          Mult = df.A * df.B)
# Out[379]: 
#    A  B   C  Diff  Mult
# 0  1  4   5     3     4
# 1  2  6   8     4    12
# 2  3  9  12     6    27

#6


1  

You could do:

你可以这样做:

df['C'] = df.sum(axis=1)

If you only want to do numerical values:

如果您只想做数值:

df['C'] = df.sum(axis=1, numeric_only=True)

#1


33  

Very simple:

df['C'] = df['A'] + df['B']

#2


25  

The simplest way would be to use DeepSpace answer. However, if you really want to use an anonymous function you can use apply:

最简单的方法是使用DeepSpace答案。但是,如果您真的想使用匿名函数,可以使用apply:

df['C'] = df.apply(lambda row: row['A'] + row['B'], axis=1)

#3


14  

You could use sum function to achieve that as @EdChum mentioned in the comment:

您可以使用sum函数来实现注释中提到的@EdChum:

df['C'] =  df[['A', 'B']].sum(axis=1)

In [245]: df
Out[245]: 
   A  B   C
0  1  4   5
1  2  6   8
2  3  9  12

#4


10  

Building a little more on Anton's answer, you can add all the columns like this:

在Anton的答案上建立更多内容,您可以添加如下所有列:

df['sum'] = df[list(df.columns)].sum(axis=1)

#5


4  

As of Pandas version 0.16.0 you can use assign as follows:

从Pandas版本0.16.0开始,您可以使用assign如下:

df = pd.DataFrame({"A": [1,2,3], "B": [4,6,9]})
df.assign(C = df.A + df.B)

# Out[383]: 
#    A  B   C
# 0  1  4   5
# 1  2  6   8
# 2  3  9  12

You can add multiple columns this way as follows:

您可以通过以下方式添加多个列:

df.assign(C = df.A + df.B,
          Diff = df.B - df.A,
          Mult = df.A * df.B)
# Out[379]: 
#    A  B   C  Diff  Mult
# 0  1  4   5     3     4
# 1  2  6   8     4    12
# 2  3  9  12     6    27

#6


1  

You could do:

你可以这样做:

df['C'] = df.sum(axis=1)

If you only want to do numerical values:

如果您只想做数值:

df['C'] = df.sum(axis=1, numeric_only=True)