I have a below data in table need to read entire data in table to pandas after performing cumulative sum and average need to generate csv file [ comma separated file with headers ]
NAME,AGE,MARKS
A1,12,40
B1,13,54
C1,15,67
D1,11,41
E1,16,59
F1,10,60
I tried to write got stuck
import cx_Oracle
import pandas as pd
try :
sq='select * from emp'
conn=cx_Oracle.cpnnect(myconnection)
fd=pd.read_sql(sql,con=conn)
fd['CUM_SUM'] = fd['MARKS'].cumsum()
fd['AVG'] = fd['MARKS'].expanding().mean()
fd.to_csv('file.csv', index=False)
except Exception as er:
print(er)
Expected output in csv file with headers
NAME,AGE,MARKS,CUM_SUM,AVG
A1,12,40,40,40
B1,13,54,94,47
C1,15,67,161,53.66
D1,11,41,202,50.5
E1,16,59,261,43.5
F1,10,60,321,45.85
When i do print(fd) , it gives below output
NAME ..CUM,AVG
A1,..,40,40
B1,..,94,47
C1,..,161,53.66
D1,..,202,50.5
E1,..,261,43.5
F1,..,321,45.85
try: except:. You'll never see what the actual error and traceback are. Then, maybecpnnectshould beconnect.