gpt4 book ai didi

python - 如何加快对 pandas 数据框的行列访问?

转载 作者:太空宇宙 更新时间:2023-11-03 16:36:34 29 4
gpt4 key购买 nike

切换到以下数据框后,我的代码遇到速度问题:

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']

我在这里做错了什么?

最佳答案

您可以使用iat :

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

时间(索引 - 字符串):

示例:

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

索引:int:

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

关于python - 如何加快对 pandas 数据框的行列访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161967/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com