Python Forum

Full Version: huge and weird values after applying some calculations
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
OK guys,

The values I got for some columns are just too huge other are in day/month format (see excel picture) and I was wondering why.
Here is my code and the file is here
here you can see the excel [Image: IKJr6go.png] and the pandas [Image: IKJrnpK.png] outputs

import pandas as pd

# Read the file
data = pd.read_csv('/your_location/for_test.csv', sep = ';')

# Set the index
data = data.set_index('Date_Time') 

# Drop the duplicated rows
data = data.drop_duplicates(keep = "first")


# Create new columns using Addition and Multiplication
data['a1'] = data['x1'] * data['y1']
data['a2'] = data['x2'] * data['y2']
data['a3'] = data['x3'] * data['y3']

data['b1'] = data['x1'] * data['z1']
data['b2'] = data['x2'] * data['z2']
data['b3'] = data['x3'] * data['z3']

data['A'] = (data['a1'] + data['a2'] + data['a3']) * 0.01 
data['B'] = (data['b1'] + data['b2'] + data['b3']) * 0.01 
data['A+B'] = data['A'] + data['B'] 

# Final dataframe
data = data[['A+B', 'A']]

save_data = data.to_csv('/your_location/output.csv', sep = ';')

data
tested with round(2) for data['A'], data['B'] and also for after addition, data['A+B'], now the data looks good on output file.

import pandas as pd
 
# Read the file
data = pd.read_csv('for_test.csv', sep = ';')
 
# Set the index
data = data.set_index('Date_Time') 
 
# Drop the duplicated rows
data = data.drop_duplicates(keep = "first")
 
print(data)
# Create new columns using Addition and Multiplication
data['a1'] = data['x1'] * data['y1']
data['a2'] = data['x2'] * data['y2']
data['a3'] = data['x3'] * data['y3']
 
data['b1'] = data['x1'] * data['z1']
data['b2'] = data['x2'] * data['z2']
data['b3'] = data['x3'] * data['z3']
 
data['A'] = (data['a1'] + data['a2'] + data['a3']) * 0.01 
data['B'] = (data['b1'] + data['b2'] + data['b3']) * 0.01 

#new added below two and another one for data['A+B'].round(2)
data['B']=data['B'].round(2)
data['A']=data['A'].round(2)

data['A+B'] = data['A'] + data['B'] 
data['A+B']=data['A+B'].round(2)


# Final dataframe
data = data[['A+B', 'A']]
 
save_data = data.to_csv('output.csv', sep = ';')
 
print(data)
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
(Dec-13-2019, 02:21 AM)sandeep_ganga Wrote: [ -> ]tested with round(2) for data['A'], data['B'] and also for after addition, data['A+B'], now the data looks good on output file.

import pandas as pd
 
# Read the file
data = pd.read_csv('for_test.csv', sep = ';')
 
# Set the index
data = data.set_index('Date_Time') 
 
# Drop the duplicated rows
data = data.drop_duplicates(keep = "first")
 
print(data)
# Create new columns using Addition and Multiplication
data['a1'] = data['x1'] * data['y1']
data['a2'] = data['x2'] * data['y2']
data['a3'] = data['x3'] * data['y3']
 
data['b1'] = data['x1'] * data['z1']
data['b2'] = data['x2'] * data['z2']
data['b3'] = data['x3'] * data['z3']
 
data['A'] = (data['a1'] + data['a2'] + data['a3']) * 0.01 
data['B'] = (data['b1'] + data['b2'] + data['b3']) * 0.01 

#new added below two and another one for data['A+B'].round(2)
data['B']=data['B'].round(2)
data['A']=data['A'].round(2)

data['A+B'] = data['A'] + data['B'] 
data['A+B']=data['A+B'].round(2)


# Final dataframe
data = data[['A+B', 'A']]
 
save_data = data.to_csv('output.csv', sep = ';')
 
print(data)
Best Regards,
Sandeep

GANGA SANDEEP KUMAR

The majority of the values in the B Column have been changed but there are others values specially being still formatted as "Jun+year" (Jun94, Jun97, Jun93)