[英]How to use pandas dataframe set_index()
Let us create a pandas dataframe
with two columns:让我们创建一个包含两列的pandas dataframe
:
lendf = pd.read_csv('/git/opencv-related/experiments/audio_and_text_files_lens.csv',
names=['path','duration'])
Here is the default numerically incrementing index
:这是默认的数字递增index
:
Let's change the index
to allow searching by the path
attribute:让我们更改index
以允许按path
属性进行搜索:
lendf.set_index(['path'])
But the index
did not change??但是index
没有变化??
How about invoking reindex()
?调用reindex()
怎么样?
lendf.reindex()
Still no change!还是没有变化!
Note that I had been referencing the source code sphinx https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.html : here is an excerpt:请注意,我一直在引用源代码 sphinx https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.set_index.ZFC35FDC70D5FC69D25E459C112175EB88E4Z 。
So then what am I misunderstanding about pandas
indexing - and how should the search/indexing by path
be set up?那么我对pandas
索引有什么误解 - 应该如何设置按path
搜索/索引?
You need to pass inplace=True
otherwise set_index
will return a new dataframe not alter the existing one您需要通过 inplace inplace=True
否则set_index
将返回一个新的 dataframe 不会改变现有的
lendf.set_index(['path'], inplace=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.