Я пытаюсь добавить сумму / сумму определенного столбца к кадру данных pandas перед записью его в файл csv. Я придумал действительно деликатное решение и подумал, может ли кто-нибудь предложить лучший подход.
`df.to_csv(out_path, index=False)
#reading content of csv file
with open(out_path,'r') as my_file:
content = my_file.read()
#adding comma in the line below adjust cell in csv file and appending content of pandas dataframe after writing aggregate total/sum.
with open(out_path,'w') as my_file:
my_file.write(',,,,'+str(df['E'].count()))
my_file.write(','+ str(df['F'].astype(float).sum()))
my_file.write(',,,,,,,,,,,,,,'+ str(df['T'].astype(float).sum()))
my_file.write('\n')
my_file.write(content)`
Любая помощь будет оценена.
Примечание: Total должен быть в верхней части файла перед заголовком.
Я ожидаю следующий вывод:
Пример кадра данных