Python 中“索引”的含义是什么?

问题描述 投票:0回答:1

我想知道写代码的时候什么时候需要“索引”。

我现在已经学会了使用seaborn包和matplotlib.pyplot来绘图。但我不明白导师给出的代码中“index”的作用是什么。

sns.countplot(data = taxisdat, x='pickup_borough', order = taxisdat['pickup_borough'].value_counts().index)

plt.xticks(range(1,len(freq.index)+1), freq.index, rotation='vertical')

python matplotlib jupyter-notebook seaborn
1个回答
0
投票

value_counts
将计算每个
pickup_borough
的值并输出一个以行政区为索引的 Series。默认情况下,该系列按计数递减排序。

taxisdat['pickup_borough'].value_counts()

pickup_borough
Manhattan    5268
Queens        657
Brooklyn      383
Bronx          99
Name: count, dtype: int64

因此,此代码是一种按照数据集中的计数顺序获取行政区列表的方法,以确保绘制的条形按此顺序:

taxisdat['pickup_borough'].value_counts().index

Index(['Manhattan', 'Queens', 'Brooklyn', 'Bronx'], dtype='object', name='pickup_borough')
© www.soinside.com 2019 - 2024. All rights reserved.