简体   繁体   English

如何使用 pandas dataframe set_index()

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM