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

Arpit Sharma

I have imported a .csv file in my python program which contains a number of columns using pandas module. 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. 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.

MSS

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. Pass approrpriate index value to get your desired result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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?

Python script imported from another module

Calculate the percentage growth from two values in a txt/csv file (Python)

Find difference of two columns in a CSV and store in third column in chunks

Python script that efficiently drops columns from a CSV file

compare columns from multiple text files, to csv column file python

Python - Calculate average for every column in a csv file

Using python dict imported from file in another python file

how to sort a csv file by two columns in python?

Python - How can you calculate values from two columns into its own column in each row?

Python: iterate through the rows of a csv and calculate date difference if there is a change in a column

How to compare each cells of one columns to each cells of another column in a csv file with python?

how to filter or manipulate columns of csv file using another column using python

Shell Script combining two columns from txt file to another!

Pandas/Python: Store values of columns into list based on value in another column

python take columns from two csv files and combine them for a new csv file

Match two columns from one file against two columns from another file and return another column from first file

read specific column from a csv file and and write into another CSV using python

Append date column to array imported from Gmail CSV File Google Apps Script

Fill new column in one dataframe with values from another, based on values in two other columns? (Python/Pandas)

A Way To Store Unknown Amount Of Separate Lists As Columns In A csv File (Python)?

Find Max & Min of one column and difference of two other columns in Python

Python error when using an object from a script in a function imported from another script

How to filter rows between two dates from CSV file using python and redirect to another file?

Python: How to calculate Difference btw Current Year and Year from Column?

Using Python to find the difference between two columns of numbers from an excel file

Python Pandas - Calculate mean using criteria from two columns

Executing python file from another script

Python script to extract data from csv file