Python : 计算从 csv 文件导入的两列的差异并存储到 python 脚本中的另一列

Python : Calculate the difference of two columns imported from a csv file and store to another column in python script

我在我的 python 程序中导入了一个 .csv 文件,其中包含许多使用 pandas 模块的列。在我的代码中,我只导入了前三列。代码和示例文件如下

import pandas as pd
fields = ['TEST ONE', 'TEST TWO', 'TEST THREE']
df1=pd.read_csv('List.csv', skipinitialspace=True, usecols=fields)

示例文件


如何在我的 python 程序中找到列 TEST ONETEST TWO 的区别并将其存储在代码中单独的 place/column/array 中,以便可以在需要时从中提取值。我想找到作为前两列之差生成的新列的平均值和最大值。

Difference = df1['TEST ONE'] - df['TEST TWO']

差异将是 pandas 系列。在那你可以使用 mean 和 max

Difference.mean()
Difference.max() 

做这样的事情。

df1['diff'] =  df1['TEST ONE'] - df1['TEST TWO']
#The Dataframe would be df1 throughout
# This will store it as a column of that same dataframe.
# When you need the difference, use that column just like normal pandas column.
mean_of_diff = df1['diff'].mean()
max_of_diff = df1['diff'].max()
# For third value of difference use the third index of dataframe
third_diff = df1.loc[2, 'diff']

注意:我使用 2 作为索引从 0 开始。索引也可以是字符串或日期。传递适当的索引值以获得您想要的结果。