Id喜欢将数据分组到.csv
文件中。我的数据如下:
code,balance
CN,999.99
CN,1.01
LS,177.77
LS,69.42
LA,200.43
WO,100
我想按代码对项目进行分组并总结类似代码的余额。所需的输出将是:
code,blance
CN,1001
LS,247.19
...
我原本使用Pandas
来完成此任务,但没有可用于将该库放在服务器上的软件包。
mydata = pd.read_csv('./tmp/temp.csv')
out = mydata.groupby('code').sum()
解决方案最好与Python 2.6
兼容。
如果这是重复的邮件,我深表歉意。其他帖子的分组方式似乎有所不同。
我也想避免在-
中执行此操作if code = x
add balance to x_total
种方式
我的解决方案:
def groupit():
groups = defaultdict(list)
with open('tmp.csv') as fd:
reader = csv.DictReader(fd)
for row in reader:
groups[row['code']].append(float(row['balance.']))
total={key:sum(groups[key]) for key in groups}
total=str(total)
total=total.replace(' ','')
total=total.replace('{','')
total=total.replace('}','')
total=total.replace("'",'')
total=total.replace(',','\n')
total=total.replace(':',',')
outfile = open('out.csv','w+')
outfile.write('code,balance\n')
outfile.write(total)
答案 0 :(得分:2)
from collections import defaultdict
import csv
groups = defaultdict(list)
with open('text.txt') as fd:
reader = csv.DictReader(fd)
for row in reader:
groups[row['code']].append(float(row['balance']))
totals = {key: sum(groups[key]) for key in groups}
print(totals)
这将输出:
{'CN': 1001.0, 'LS': 247.19, 'LA': 200.43, 'WO': 100.0}
from collections import defaultdict
import csv
groups = defaultdict(list)
with open('text.txt') as fd:
reader = csv.DictReader(fd)
for row in reader:
groups[row['code']].append(float(row['balance']))
totals = dict((key, sum(groups[key])) for key in groups)
print(totals)
答案 1 :(得分:1)
这就是我要做的事情:
with open("data.csv", 'r') as f:
data = f.readlines()
result = {}
for val in range(1, len(data)-1):
x = data[val].split(",")
if x[0] not in result:
result[x[0]] = float(x[1].replace('\n', ""))
else:
result[x[0]] = result[x[0]] + float(x[1].replace('\n', ""))
result
词典将具有感兴趣的值,然后可以将其保存为csv。
import csv
with open('mycsvfile.csv', 'wb') as f: # Just use 'w' mode in 3.x
w = csv.DictWriter(f, result.keys())
w.writeheader()
w.writerow(result)
希望这会有所帮助:)