简体   繁体   中英

How to use pandas dataframe set_index()

Let us create a pandas dataframe with two columns:

lendf = pd.read_csv('/git/opencv-related/experiments/audio_and_text_files_lens.csv',
        names=['path','duration'])

Here is the default numerically incrementing index :

在此处输入图像描述

Let's change the index to allow searching by the path attribute:

lendf.set_index(['path'])

But the index did not change??

在此处输入图像描述

How about invoking 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:

在此处输入图像描述

So then what am I misunderstanding about pandas indexing - and how should the search/indexing by path be set up?

You need to pass inplace=True otherwise set_index will return a new dataframe not alter the existing one

lendf.set_index(['path'], inplace=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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