gpt4 book ai didi

python - 如何正确获取 pandas : loc[index, 列中的单个单元格] VS get_value(index,column)

转载 作者:行者123 更新时间:2023-11-28 22:32:52 26 4
gpt4 key购买 nike

为了从 pandas DataFrame 获取单个单元格,使用哪种方法更好(在性能和可靠性方面):get_value() 或 loc[]?

最佳答案

您可以在 docs 中找到信息最后:

For getting a value explicitly (equiv to deprecated df.get_value('a','A'))

# this is also equivalent to ``df1.at['a','A']``
In [55]: df1.loc['a', 'A']
Out[55]: 0.13200317033032932

但如果使用它则没有警告。

但是如果检查Index.get_value :

Fast lookup of value from 1-dimensional ndarray. Only use this if you know what you’re doing

所以我认为更好的方法是使用 iat , at , loc , ix .

时间:

df = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6],
'C':[7,8,9],
'D':[1,3,5],
'E':[5,3,6],
'F':[7,4,3]})

print (df)

In [93]: %timeit (df.loc[0, 'A'])
The slowest run took 6.40 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 177 µs per loop

In [96]: %timeit (df.at[0, 'A'])
The slowest run took 17.01 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7.61 µs per loop

In [94]: %timeit (df.get_value(0, 'A'))
The slowest run took 23.49 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 3.36 µs per loop

关于python - 如何正确获取 pandas : loc[index, 列中的单个单元格] VS get_value(index,column),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40524086/

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