我正在尝试将特定列的 Sum/Count 添加到 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)`
任何帮助,将不胜感激。
注意:总计必须在文件顶部的标题之前。
我期待以下输出:
饮歌长啸
拉风的咖菲猫
相关分类