Pandas:如何找到两列之间的差异
要查找 pandas DataFrame 中两列之间的差异,可以使用以下语法:
df[' difference '] = df[' column1 '] - df[' column2 ']
以下示例展示了如何在实践中使用此语法。
示例 1:查找两列之间的差异
假设我们有以下 pandas DataFrame,显示两个区域(A 和 B)连续八个销售周期的总销售额:
import pandas as pd #createDataFrame df = pd. DataFrame ({' period ': [1, 2, 3, 4, 5, 6, 7, 8], ' A_sales ': [12, 14, 15, 13, 18, 20, 19, 24], ' B_sales ': [14, 19, 20, 22, 24, 20, 17, 23]}) #view DataFrame df period A_sales B_sales 0 1 12 14 1 2 14 19 2 3 15 20 3 4 13 22 4 5 18 24 5 6 20 20 6 7 19 17 7 8 24 23
下面的代码展示了如何计算每个销售周期的B区和A区销售额之间的差异:
#add new column to represent difference between B sales and A sales df[' diff '] = df[' B_sales '] - df[' A_sales '] #view DataFrame df period A_sales B_sales diff 0 1 12 14 2 1 2 14 19 5 2 3 15 20 5 3 4 13 22 9 4 5 18 24 6 5 6 20 20 0 6 7 19 17 -2 7 8 24 23 -1
我们还可以使用pandas.Series.abs()函数计算销售额的绝对差异:
#add new column to represent absolute difference between B sales and A sales df[' diff '] = pd. Series . abs (df[' B_sales '] - df[' A_sales ']) #view DataFrame df period A_sales B_sales diff 0 1 12 14 2 1 2 14 19 5 2 3 15 20 5 3 4 13 22 9 4 5 18 24 6 5 6 20 20 0 6 7 19 17 2 7 8 24 23 1
示例2:根据条件查找列之间的差异
我们还可以过滤 DataFrame 以仅显示列之间的差异小于或大于特定值的行。
例如,以下代码仅返回区域 A 的销售额大于区域 B 的销售额的行:
#add new column to represent difference between B sales and A sales df[' diff '] = df[' B_sales '] - df[' A_sales '] #display rows where sales in region A is greater than sales in region B df[df[' diff ']< 0 ] period A_sales B_sales diff 6 7 19 17 -2 7 8 24 23 -1