我有一个像这样的csv文件:
meshcode2,result
30548625,218.14
30548616,318.75
30548616,404.53
30548616,343.85
30647133,78.43
30647133,125.53
30548615,314.68
30548615,314.68
30548615,264.50
30548615,264.50
30548626,98.03
30548615,166.04
30548615,114.12
30548615,104.67
30548616,470.25
我想找到这个csv文件的中位数并存储在csv文件中,所以我这样做了:
import pandas as pd
import csv
df = pd.read_csv('001_meshcode2_result_sale.csv')
result = df.groupby('meshcode2').median()
with open('result.csv', 'w', newline='') as f:
writer = csv.writer(f)
for data in result:
writer.writerows(data)
当我打开结果文件时,我只收到"结果"字母,如何将整个结果写入result.csv文件
答案 0 :(得分:2)
Simplier是为as_index=False
的列添加参数meshcode2
,然后是to_csv
:
result = df.groupby('meshcode2', as_index=False).median()
print (result)
meshcode2 result
0 30548615 264.50
1 30548616 374.19
2 30548625 218.14
3 30548626 98.03
4 30647133 101.98
result.to_csv('result.csv', index=False)