2016-05-11 5 views
1

私はデータフレームを、以下に切り替えた後、私は自分のコードでスピードの問題が生じています:pandasデータフレームへの列のアクセスをどのように高速化できますか?

df = pd.DataFrame(data=products, columns=['pk', 'product_name', 'category_name', 'brand_name']) 
df.set_index(['pk'], inplace=True) 

これは私がデータフレームを使用する唯一の場所です。 'pk'は整数です。

  category = self.product_list.iloc[int(prod)-1]['category_name'] 
      brand = self.product_list.iloc[int(prod)-1]['brand_name'] 

私はここで間違っていますか?

答えて

1

あなたはiatを使用することができます。

print product_list.category_name.iat[int(prod)-1] 
print product_list.brand_name.iat[int(prod)-1] 

タイミングindexからstring):

サンプル:

product_list = pd.DataFrame({'brand_name': {'r': 'r', 'g': 't', 'w': 'i'}, 
          'category_name': {'r': 's', 'g': 'f', 'w': 'a'}}) 
print product_list 
    brand_name category_name 
g   t    f 
r   r    s 
w   i    a 

In [242]: %timeit product_list.iloc[int(prod)-1]['category_name'] 
The slowest run took 8.27 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 82.7 µs per loop 

In [243]: %timeit product_list.brand_name.iat[int(prod)-1] 
The slowest run took 16.01 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 9.96 µs per loop 

indexint

product_list = pd.DataFrame({'brand_name': {0: 't', 1: 'r', 2: 'i'}, 
          'category_name': {0: 'f', 1: 's', 2: 'a'}}) 
print product_list 
    brand_name category_name 
0   t    f 
1   r    s 
2   i    a 

In [250]: %timeit product_list.iloc[int(prod)-1]['category_name'] 
The slowest run took 8.24 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 84.7 µs per loop 

In [251]: %timeit product_list.brand_name.iat[int(prod)-1] 
The slowest run took 24.17 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 9.86 µs per loop 
+0

ご協力いただきありがとうございます。 –

+0

うれしいことができますよ!がんばろう! – jezrael

+1

解決策を忘れないでください。ありがとう。 – jezrael

関連する問題