2016-01-24 94 views
0

我有一個有很多行的大熊貓數據框。如何禁止在python熊貓數據框中生成行號?

id1 id2 id3 count 
0 a  b a 1 
1 a  b b 2 
2 a  b c 3 

我想計算行出現次數。這是我正在努力做到這一點。

import pandas as pd 
from collections import Counter 

pdf = pd.DataFrame.from_records(data_tupl) 
cnts = Counter(pdf.itertuples()) 

不幸的是itertuples()輸出行號(0, 'a', 'b', 'a', 1)我絕對不需要。我當然可以將其切斷,但這需要一箇中間步驟,這會降低性能。是否可以抑制熊貓行輸出?

回答

2

對於很多重複的大DataFrames,它可能更快地使用熊貓方法groupby/count行那將是比使用collections.Counter

In [75]: df = pd.DataFrame(np.random.randint(2, size=(10000,4))) 

In [76]: df.reset_index().groupby(list(df.columns)).count().to_dict('dict')['index'] 
Out[76]: 
{(0, 0, 0, 0): 639, 
(0, 0, 0, 1): 621, 
(0, 0, 1, 0): 658, 
(0, 0, 1, 1): 595, 
(0, 1, 0, 0): 601, 
(0, 1, 0, 1): 640, 
(0, 1, 1, 0): 643, 
(0, 1, 1, 1): 632, 
(1, 0, 0, 0): 637, 
(1, 0, 0, 1): 644, 
(1, 0, 1, 0): 574, 
(1, 0, 1, 1): 642, 
(1, 1, 0, 0): 612, 
(1, 1, 0, 1): 667, 
(1, 1, 1, 0): 588, 
(1, 1, 1, 1): 607} 

In [77]: collections.Counter(df.itertuples(index=False)) 
Out[77]: Counter({Pandas(_0=1, _1=1, _2=0, _3=1): 667, Pandas(_0=0, _1=0, _2=1, _3=0): 658, Pandas(_0=1, _1=0, _2=0, _3=1): 644, Pandas(_0=0, _1=1, _2=1, _3=0): 643, Pandas(_0=1, _1=0, _2=1, _3=1): 642, Pandas(_0=0, _1=1, _2=0, _3=1): 640, Pandas(_0=0, _1=0, _2=0, _3=0): 639, Pandas(_0=1, _1=0, _2=0, _3=0): 637, Pandas(_0=0, _1=1, _2=1, _3=1): 632, Pandas(_0=0, _1=0, _2=0, _3=1): 621, Pandas(_0=1, _1=1, _2=0, _3=0): 612, Pandas(_0=1, _1=1, _2=1, _3=1): 607, Pandas(_0=0, _1=1, _2=0, _3=0): 601, Pandas(_0=0, _1=0, _2=1, _3=1): 595, Pandas(_0=1, _1=1, _2=1, _3=0): 588, Pandas(_0=1, _1=0, _2=1, _3=0): 574}) 

In [78]: %timeit collections.Counter(df.itertuples(index=False)) 
100 loops, best of 3: 12.8 ms per loop 

In [79]: %timeit df.reset_index().groupby(list(df.columns)).count().to_dict('dict')['index'] 
100 loops, best of 3: 3.74 ms per loop 

對於少數人的數據幀重複,速度是可比的:

In [80]: df = pd.DataFrame(np.random.randint(1000, size=(10000,4))) 

In [81]: %timeit collections.Counter(df.itertuples(index=False)) 
100 loops, best of 3: 11.2 ms per loop 

In [82]: %timeit df.reset_index().groupby(list(df.columns)).count().to_dict('dict')['index'] 
100 loops, best of 3: 11.7 ms per loop