I have an excel file that I have imported into Python using pandas and it has two columns, purchase price and sales price. They are both number values. I want to use python to automatically do the math for me to find the difference between the two, in this case I want it to be Sales Price minus Purchase Price. Is it possible to write a script for this? Thanks in advance for any help.
CodePudding user response:
# create new column named profit
df['profit'] = df['Sales Price'] - df['Purchase Price']
CodePudding user response:
import pandas as pd
Read the Excel file into a pandas DataFrame
df = pd.read_excel('file.xlsx')
Find the difference between the two columns
df['difference'] = df['column1'] - df['column2']
Print the resulting DataFrame
print(df)
CodePudding user response:
Absolutely, you will need to iterate over the columns you have pulled into pandas similar to the psuedo code below
import pandas as pd
file = "path/to/file.csv"
df = pd.read_csv(file)
mapped_sales_price = df['Sales Price'].values()
mapped_purchase_price = df['Purchase Price'].values()
profit = [x - y for x in mapped_sales_price for y in mapped_purchase_price]
print(profit)
From here you can easily create a new column in the pandas dataframe with the profit to see the profit per row, however if you are only interested in the total profit of the entire csv entries you have recorded then you can do something like this with the profit array (list)
total_profit = sum(int(i for i in profit))
this will convert any strings to integers , if you have floating point values then you would need something like this
import math
normalized_profit_values = list(map( float, profit))
total_profit = math.fsum(normalized_profit_values)