0

I am trying to read the file, then i would like to done the calculation to write back to the same file. But the result will replace the ori existing data, how can i change it? Please help me

import pandas as pd

df = pd.read_csv(r'C:\Users\Asus\Downloads\Number of Employed Persons by Status In Employment, Malaysia.csv')

print(df.to_string())

mean1 = df['Value'].mean()
sum1 = df['Value'].sum()

print ('Mean Value: ' + str(mean1))
print ('Sum of Value: ' + str(sum1))

df = pd.DataFrame([['Mean Value: ' + str(mean1)], ['Sum of Value: ' + str(sum1)]])

df.to_csv(r'C:\Users\Asus\Downloads\Number of Employed Persons by Status In Employment, Malaysia.csv', index=False)

print(df)
Mustafa Aydın
  • 17,645
  • 4
  • 15
  • 38
  • Are you sure you want to do this? The calculated values are aggregates and refer to whole columns of the original CSV. You might want to store these summary statistics in a different place. – nocibambi Jun 05 '21 at 07:37
  • It might also help if you could share examples of your data based on [these suggestions](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – nocibambi Jun 05 '21 at 07:39

1 Answers1

0

Do you want to add the data at the bottom of the file?

Override the data is not the best approach, in my opinion, but this is one solution:

import pandas as pd

df = pd.read_csv('data.csv')

mean1 = df['Value'].mean() 
sum1 = df['Value'].sum()

df.loc[df.index.max() + 1] = ['Mean','Sum']
df.loc[df.index.max() + 1] = [mean1, sum1]

df.to_csv('data.csv', index=False)

Another option could be: Save all into an xlsx at the end (is better load the data from CSV if there is a large of data) and keep the dataframe1 in a first sheet, and the analysis on a second sheet.

mndv
  • 130
  • 8