Hi all, I am comparing 2 data frames. please can someone advice how I can limit numbers to 2 decimal points and get rid of word Timestamp. out put of the code is pasted below
When I paste the code here the formatting is messed up. but the code works fine in sublime Test
Python program to read an excel file
import pandas as pd
import numpy as np
import warnings
Give the location of the file
path = ‘C:/Users/ambar/OneDrive/Desktop/dummy/a1b.xlsx’
df= pd.read_excel(path, sheet_name=‘Sheet1’, usecols=[‘Date’,‘name’, ‘myname’, ‘fx’])
ds= pd.read_excel(path, sheet_name=‘Sheet1’, usecols=[‘Date’])
df2= pd.read_excel(path, sheet_name=‘Sheet1’, usecols=[‘Date’,‘name1’, ‘myname1’, ‘fx1’])
df.head()
ds[“Date”]=pd.to_datetime(ds.Date,format=“%Y-%m-%d”)
ds.index=ds[‘Date’]
df1= df.copy()
Calculate percentage change in dataframe
df[[‘name’,‘myname’,‘fx’]] = df[[‘name’,‘myname’,‘fx’]].pct_change(periods = 5)
print(“Number of Exceptions in columns: Date Name myname fx are:”)
print(“Number of Exceptions are:”, sum(np.array(df)>np.array(df2)))
print (“\n\n”)
list_of_column_names = list(df.columns)
displaying the list of column names
print(list_of_column_names)
a=np.array(df)
c=np.array(df1)
b = np.where(df[‘name’]> df2[‘name1’])
print (a[b])
print (“\n\n”)
print(c[b])
print (“\n\n”)
b = np.where(df[‘myname’]> df2[‘myname1’])
print (a[b])
print (“\n\n”)
print(c[b])
---------------------------output---------------------------------------------------------
[‘Date’, ‘name’, ‘myname’, ‘fx’]
[[Timestamp(‘2000-11-16 00:00:00’) 5.0 19.0 -0.5]
[Timestamp(‘2000-11-17 00:00:00’) 2.5 15.5 0.3999999999999999]
[Timestamp(‘2000-11-18 00:00:00’) 1.6666666666666665 1.0 0.0]
[Timestamp(‘2000-11-19 00:00:00’) 1.25 0.33333333333333326
-0.19999999999999996]
[Timestamp(‘2000-11-27 00:00:00’) 3.666666666666667 1.6666666666666665
-0.19999999999999996]
[Timestamp(‘2000-11-28 00:00:00’) 2.8333333333333335 0.0
0.33333333333333326]
[Timestamp(‘2000-11-29 00:00:00’) 3.0 -0.33333333333333337
-0.33333333333333337]]