📜  如何在 Pandas DataFrame 中减去两列?

📅  最后修改于: 2022-05-13 01:55:44.424000             🧑  作者: Mango

如何在 Pandas DataFrame 中减去两列?

在本文中,我们将讨论如何在Python中减去 pandas 数据框中的两列。

正在使用的数据框:

方法一:直接法

这是__getitem__方法语法( [] ),它允许您使用列名直接访问数据框的列。

示例:减去 Pandas 数据框中的两列

Python3
import numpy as np
import pandas as pd
  
data = np.arange(0, 20).reshape(4, 5)
  
  
df1 = pd.DataFrame(data,
                   index=['Row 1', 'Row 2', 'Row 3', 'Row 4'],
                   columns=['Column 1', 'Column 2', 'Column 3',
                            'Column 4', 'Column 5'])
  
# using our previous example
# now let's subtract the values of two columns
df1['Column 1'] - df1['Column 2']


Python3
import numpy as np
import pandas as pd
  
def diff(a, b):
    return b - a
  
data = np.arange(0, 20).reshape(4, 5)
  
  
df = pd.DataFrame(data,
                  index=['Row 1', 'Row 2', 'Row 3', 'Row 4'],
                  columns=['Column 1', 'Column 2', 'Column 3',
                           'Column 4', 'Column 5'])
  
  
df['Difference_2_1'] = df.apply(
    lambda x: diff(x['Column 2'], x['Column 2']), axis=1)


Python3
import pandas as pd
import numpy as np
  
data = np.arange(0, 20).reshape(4, 5)
  
  
df = pd.DataFrame(data,
                  index=['Row 1', 'Row 2', 'Row 3', 'Row 4'],
                  columns=['Column 1', 'Column 2', 'Column 3',
                           'Column 4', 'Column 5'])
  
  
df['diff_3_4'] = df.apply(lambda x: x['Column 3'] - x['Column 4'], axis=1)
df


Python3
import numpy as np
import pandas as pd
  
data = np.arange(0, 20).reshape(4, 5)
  
  
df = pd.DataFrame(data,
                  index=['Row 1', 'Row 2', 'Row 3', 'Row 4'],
                  columns=['Column 1', 'Column 2', 'Column 3',
                           'Column 4', 'Column 5'])
  
  
df = df.assign(diff_1_5=df['Column 1'] - df['Column 5'])
  
df


输出:

方法 2:定义函数

我们可以创建一个专门用于减去列的函数,将列数据作为参数,然后使用 apply 方法将其应用于整个列的所有数据点。

示例:减去 Pandas 数据框中的两列

Python3

import numpy as np
import pandas as pd
  
def diff(a, b):
    return b - a
  
data = np.arange(0, 20).reshape(4, 5)
  
  
df = pd.DataFrame(data,
                  index=['Row 1', 'Row 2', 'Row 3', 'Row 4'],
                  columns=['Column 1', 'Column 2', 'Column 3',
                           'Column 4', 'Column 5'])
  
  
df['Difference_2_1'] = df.apply(
    lambda x: diff(x['Column 2'], x['Column 2']), axis=1)

输出 :

方法 3:使用 apply()

由于我们要执行的操作很简单,我们可以直接使用apply()方法而无需显式定义函数。将轴参数提供为1以访问列。

示例:在 Pandas Dataframe 中减去两列

Python3

import pandas as pd
import numpy as np
  
data = np.arange(0, 20).reshape(4, 5)
  
  
df = pd.DataFrame(data,
                  index=['Row 1', 'Row 2', 'Row 3', 'Row 4'],
                  columns=['Column 1', 'Column 2', 'Column 3',
                           'Column 4', 'Column 5'])
  
  
df['diff_3_4'] = df.apply(lambda x: x['Column 3'] - x['Column 4'], axis=1)
df

输出:

方法 4:使用分配方法

assign()方法将新列分配给 DataFrame,返回一个新对象(副本),其中新列添加到原始列。

示例:减去 Pandas 数据框中的两列

Python3

import numpy as np
import pandas as pd
  
data = np.arange(0, 20).reshape(4, 5)
  
  
df = pd.DataFrame(data,
                  index=['Row 1', 'Row 2', 'Row 3', 'Row 4'],
                  columns=['Column 1', 'Column 2', 'Column 3',
                           'Column 4', 'Column 5'])
  
  
df = df.assign(diff_1_5=df['Column 1'] - df['Column 5'])
  
df

输出 :