计算迭代中Pandas数据帧的平均值

时间:2014-02-28 12:56:50

标签: python file csv file-io pandas

我有一个像这样的巨大的CSV文件

代码,持续时间

101,32

205,111

722,33

205,67

722,33

205,241

现在我正在阅读大块文件,因为文件非常大。如何计算每个代码的平均持续时间并将其保存到CSV文件?

由于

3 个答案:

答案 0 :(得分:1)

对每个数据框使用groupby.sizegroupby.sum,然后将其缩减为结果:

import numpy as np
import pandas as pd

c = np.random.randint(100, 10000, 100000)
d = np.random.rand(100000)

df = pd.DataFrame({"c":c, "d":d})
r1 = df.groupby("c").d.mean()

counts = []
sums = []
for i in range(10):
    df2 = df[i*10000:(i+1)*10000]
    g = df2.groupby("c").d
    counts.append(g.size())
    sums.append(g.sum())

from functools import partial
func = partial(pd.Series.add, fill_value=0)
r2 = reduce(func,  sums) / reduce(func, counts).astype(float)

您还可以使用以下代码进行最后一步:

r3 = pd.concat(sums, axis=1).sum(axis=1) / pd.concat(counts, axis=1).sum(axis=1).astype(float)

检查结果:

print np.allclose(r1, r2)
print np.allclose(r1, r3)

答案 1 :(得分:1)

不是pandas但是它有效且内存效率很高。

import csv
from collections defaultdict

code_counts = defaultdict(int)
code_durations = defaultdict(int)
with open('yourfile.csv', 'rb') as f:
    reader = csv.reader(f)
    next(reader) # discard header row
    for code, duration in reader:
        code_counts[code] += 1
        code_durations[code] += int(duration)    
code_averages = {code: code_duratons[code] / float(code_counts[code]) for code in code_counts}

答案 2 :(得分:1)

您可以按代码分组并存储'Code''Duration'的计数和总和;像这样的东西:

import pandas as pd

def f(g):
    return pd.DataFrame({'count': [g.shape[0]], 'sum': [g['Duration'].sum()]})

reader = pd.read_csv('data.csv',chunksize=2)
acc = pd.DataFrame({})
for chunk in reader:
    acc = acc.add(chunk.groupby('Code').apply(f).reset_index(level=1,drop=True),fill_value=0)

acc['avg'] = acc['sum']/acc['count']
print acc

acc.to_csv('avg_codes.csv',cols=['avg'],index_label='Code')

终端输出:

      count  sum         avg
Code                        
101       1   32   32.000000
205       3  419  139.666667
722       2   66   33.000000

文件输出 avg_codes.csv

Code,avg
101,32.0
205,139.66666666666666
722,33.0