Home > other >  Python for eligible for statistical calculation combined (), and add new rows written at the end of
Python for eligible for statistical calculation combined (), and add new rows written at the end of

Time:01-01

Ladies and gentlemen, the existing table contains 2 columns (the first listed as "payment amount", the second as "order status"), the need to implement:
1. The "order status" for "failure" and "refunded" screening and statistics the sum total of the sum of payment amount will be calculated;
2. Due to the amount of the list is not fixed, need to calculate the sum of the added automatically written to the end of the data display;

How do I achieve?

CodePudding user response:

The CSV?

CodePudding user response:

No, is a XLSX file

CodePudding user response:

The import pandas as pd


Df=pd read_csv (r "new CSV", encoding="GB2312")
Fail=df [(df/" order status "==" failure ") | (df/" order status "==" refunded ")]
Df. Loc ["]=[" total ", STR (sum (fail (" payment amount "))))
Print (df)

Df. To_csv (r 'result. CSV, index=False, sep=', ')

CodePudding user response:

The
reference 3 floor CreateBig response:
import pandas as pd


Df=pd read_csv (r "new CSV", encoding="GB2312")
Fail=df [(df/" order status "==" failure ") | (df/" order status "==" refunded ")]
Df. Loc ["]=[" total ", STR (sum (fail (" payment amount "))))
Print (df)

Df. To_csv (r 'result. CSV, index=False, sep=', ')


How do I realize: due to the number of the source file list is not fixed, need to calculate the sum of the added automatically save the end of the source file data written to the display? Rather than a new document

CodePudding user response:

refer to the second floor summersnow_ay response:
no, is a XLSX file
that don't convert to CSV or change read_csv read_excel

CodePudding user response:

The import pandas as pd


Df=pd read_excel (r "new. XLSX")
Fail=df [(df/" order status "==" failure ") | (df/" order status "==" refunded ")]
Df. Loc ["]=[" total ", STR (sum (fail (" payment amount "))))
Print (df)

Df. To_excel (r 'new. XLSX, index=False)
  • Related