pandas 在熊猫中建立索引有什么意义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27238066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the point of indexing in pandas?
提问by user3659451
Can someone point me to a link or provide an explanation of the benefits of indexing in pandas? I routinely deal with tables and join them based on columns, and this joining/merging process seems to re-index things anyway, so it's a bit cumbersome to apply index criteria considering I don't think I need to.
有人可以指向我的链接或解释在Pandas中建立索引的好处吗?我经常处理表并根据列连接它们,并且这个连接/合并过程似乎无论如何都会重新索引事物,因此考虑到我认为我不需要应用索引标准有点麻烦。
Any thoughts on best-practices around indexing?
关于索引的最佳实践有什么想法吗?
回答by unutbu
Like a dict, a DataFrame's index is backed by a hash table. Looking up rows based on index values is like looking up dict values based on a key.
像字典一样,DataFrame 的索引由哈希表支持。根据索引值查找行就像根据键查找字典值。
In contrast, the values in a column are like values in a list.
相反,列中的值类似于列表中的值。
Looking up rows based on index values is faster than looking up rows based on column values.
根据索引值查找行比根据列值查找行要快。
For example, consider
例如,考虑
df = pd.DataFrame({'foo':np.random.random(), 'index':range(10000)})
df_with_index = df.set_index(['index'])
Here is how you could look up any row where the df['index']column equals 999.
Pandas has to loop through every value in the column to find the ones equal to 999.
以下是查找df['index']列等于 999 的任何行的方法。Pandas 必须遍历列中的每个值才能找到等于 999 的值。
df[df['index'] == 999]
# foo index
# 999 0.375489 999
Here is how you could lookup any row where the index equals 999. With an index, Pandas uses the hash value to find the rows:
以下是查找索引等于 999 的任何行的方法。对于索引,Pandas 使用哈希值来查找行:
df_with_index.loc[999]
# foo 0.375489
# index 999.000000
# Name: 999, dtype: float64
Looking up rows by index is much faster than looking up rows by column value:
按索引查找行比按列值查找行要快得多:
In [254]: %timeit df[df['index'] == 999]
1000 loops, best of 3: 368 μs per loop
In [255]: %timeit df_with_index.loc[999]
10000 loops, best of 3: 57.7 μs per loop
Note however, it takes time to build the index:
但是请注意,构建索引需要时间:
In [220]: %timeit df.set_index(['index'])
1000 loops, best of 3: 330 μs per loop
So having the index is only advantageous when you have many lookups of this type to perform.
因此,只有当您要执行许多这种类型的查找时,拥有索引才有好处。
Sometimes the index plays a role in reshaping the DataFrame. Many functions, such as set_index, stack, unstack, pivot, pivot_table, melt,
lreshape, and crosstab, all use or manipulate the index.
Sometimes we want the DataFrame in a different shape for presentation purposes, or for join, mergeor groupbyoperations. (As you note joining can also be done based on column values, but joining based on the index is faster.) Behind the scenes, join, mergeand groupbytake advantage of fast index lookups when possible.
有时,索引会在重塑 DataFrame 中发挥作用。许多功能,如set_index,stack,unstack,pivot,pivot_table,melt,
lreshape,和crosstab,全部使用或操纵的指数。有时我们希望 DataFrame 具有不同的形状以用于演示目的,或用于join、merge或groupby操作。(正如您所注意到的,连接也可以基于列值完成,但基于索引的连接速度更快。)在幕后join,merge和groupby尽可能利用快速索引查找。
Time series have resample, asfreqand interpolatemethods whose underlying implementations take advantage of fast index lookups too.
时间序列已经resample,asfreq并且interpolate其基本实现拍摄快速索引查找的优势太方法。
So in the end, I think the origin of the index's usefulness, why it shows up in so many functions, is due to its ability to perform fast hash lookups.
所以最后,我认为索引有用的起源,为什么它出现在这么多函数中,是因为它能够执行快速哈希查找。

