简体   繁体   English

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

[英]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 ONETEST 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.

相关问题 使用 Python 3.8 将一个 CSV 文件中的一列(向量)与另一个 CSV 文件中的两列(向量和数组)进行比较 - Compare one column (vector) from one CSV file with two columns (vector and array) from another CSV file using Python 3.8 如何仅排序和存储两个.CSV 文件中的前 3 个位置,然后使用 Python 将它们存储到一个.CSV 文件中的两列? - How to sort and store only the top 3 locations from two .CSV files and then store them into two columns in one .CSV file using Python? 将 csv 文件中的两列数据一起添加到 python 中同一 csv 文件中的新列中 - Adding two columns of data together from a csv file into a new column in the same csv file in python python中如何逐列计算差值? - How to calculate the difference between columns by column in python? 使用 python 查找两个 csv 文件列之间的差异 - Find difference between two csv file column wise using python 比较两个csv文件中的列并将其写入python中的另一个文件 - Compare columns in two csv files and write it to another file in python Python-对从CSV文件导入的数组求和 - Python - sum an array imported from a CSV file 使用Python将列从一个.csv添加到另一个.csv文件 - Add column from one .csv to another .csv file using Python Python 3,将特定列从一个csv文件复制到另一个csv - Python 3, Copy specific column from one csv file to another csv Python3:如何将短脚本中的信息存储在CSV文件中? - Python3: How to store information from short script in a CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM