[英]Python : Calculate the difference of two columns imported from a csv file and store to another column in python script
I have imported a .csv file in my python program which contains a number of columns using pandas module. 我已经在python程序中导入了一个.csv文件,其中使用pandas模块包含了许多列。 In my code, I just imported the first three columns.
在我的代码中,我只是导入了前三列。 The code and the sample file are as follows.
代码和示例文件如下。
import pandas as pd
fields = ['TEST ONE', 'TEST TWO', 'TEST THREE']
df1=pd.read_csv('List.csv', skipinitialspace=True, usecols=fields)
sample file 样本文件
How can I find the difference of the columns TEST ONE and TEST TWO in my python program and store it in separate place/column/array inside the code so that the values can be extracted from it whenever needed. 如何在我的python程序中找到列TEST ONE和TEST TWO 2的区别,并将其存储在代码内的单独位置/列/数组中,以便可以在需要时从中提取值。 I want to find the mean and the maximum value of the new column which is generated as the difference of the first two columns.
我想找到作为前两列之差而生成的新列的均值和最大值。
Do something like this. 做这样的事情。
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']
Note: I have used 2 as index starts from 0. Also index can be a string or date as well. 注意:我使用2作为从0开始的索引。索引也可以是字符串或日期。 Pass approrpriate index value to get your desired result.
传递适当的索引值以获得所需的结果。
Difference = df1['TEST ONE'] - df['TEST TWO']
Difference will be pandas series. 区别将是熊猫系列。 on that you can use mean and max
在那你可以使用均值和最大值
Difference.mean()
Difference.max()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.