公众号:尤而小屋
作者:Peter
编辑:Peter
大家好,我是Peter~
本文记录的是Pandas中10种单层索引的常用属性,文末有汇总的常见属性,建议收藏!
[图片上传失败...(image-1b2eef-1650039172740)]
10种索引
快速回顾Pandas中10种单层索引的创建:
pd.Index
In [1]:
import pandas as pd
import numpy as np
In [2]:
# 指定类型和名称
s1 = pd.Index([1,2,3,4,5,6,7],
dtype="int",
name="Peter")
s1
Out[2]:
Int64Index([1, 2, 3, 4, 5, 6, 7], dtype='int64', name='Peter')
pd.RangeIndex
指定整数范围内的不可变索引
In [3]:
s2 = pd.RangeIndex(0,20,2)
s2
Out[3]:
RangeIndex(start=0, stop=20, step=2)
pd.Int64Index
64位整数型索引
In [4]:
s3 = pd.Int64Index([1,2,3,4,5,6,7,8],name="Peter")
s3
Out[4]:
Int64Index([1, 2, 3, 4, 5, 6, 7, 8], dtype='int64', name='Peter')
pd.UInt64Index
无符号整数索引
In [5]:
s4 = pd.UInt64Index([1, 2.0, 3, 4],name="Tom")
s4
Out[5]:
UInt64Index([1, 2, 3, 4], dtype='uint64', name='Tom')
pd.Float64Index
64位浮点型的索引
In [6]:
s5 = pd.Float64Index([1.5, 2.4, 3.7, 4.9],name="peter")
s5
Out[6]:
Float64Index([1.5, 2.4, 3.7, 4.9], dtype='float64', name='peter')
pd.IntervalIndex
新的间隔索引 IntervalIndex 通常使用 interval_range()函数来进行构造,它使用的是数据或者数值区间,基本用法:
In [7]:
s6 = pd.interval_range(start=0, end=6, closed="left")
s6
Out[7]:
IntervalIndex([[0, 1), [1, 2), [2, 3), [3, 4), [4, 5), [5, 6)],
closed='left',
dtype='interval[int64]')
pd.CategoricalIndex
In [8]:
s7 = pd.CategoricalIndex(
# 待排序的数据
["S","M","L","XS","M","L","S","M","L","XL"],
# 指定分类顺序
categories=["XS","S","M","L","XL"],
# 排需
ordered=True,
# 索引名字
name="category"
)
s7
Out[8]:
CategoricalIndex(['S', 'M', 'L', 'XS', 'M', 'L', 'S', 'M', 'L', 'XL'], categories=['XS', 'S', 'M', 'L', 'XL'], ordered=True, name='category', dtype='category')
pd.DatetimeIndex
以时间和日期作为索引,通过date_range函数来生成,具体例子为:
In [9]:
# 日期作为索引,D代表天
s8 = pd.date_range("2022-01-01",periods=6, freq="D")
s8
Out[9]:
DatetimeIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
'2022-01-05', '2022-01-06'],
dtype='datetime64[ns]', freq='D')
pd.PeriodIndex
pd.PeriodIndex是一个专门针对周期性数据的索引,方便针对具有一定周期的数据进行处理,具体用法如下:
In [10]:
s9 = pd.PeriodIndex(['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04'], freq = '2H')
s9
Out[10]:
PeriodIndex(['2022-01-01 00:00', '2022-01-02 00:00', '2022-01-03 00:00',
'2022-01-04 00:00'],
dtype='period[2H]', freq='2H')
pd.TimedeltaIndex
In [11]:
data = pd.timedelta_range(start='1 day', end='3 days', freq='6H')
data
Out[11]:
TimedeltaIndex(['1 days 00:00:00', '1 days 06:00:00', '1 days 12:00:00',
'1 days 18:00:00', '2 days 00:00:00', '2 days 06:00:00',
'2 days 12:00:00', '2 days 18:00:00', '3 days 00:00:00'],
dtype='timedelta64[ns]', freq='6H')
In [12]:
s10 = pd.TimedeltaIndex(data)
s10
Out[12]:
TimedeltaIndex(['1 days 00:00:00', '1 days 06:00:00', '1 days 12:00:00',
'1 days 18:00:00', '2 days 00:00:00', '2 days 06:00:00',
'2 days 12:00:00', '2 days 18:00:00', '3 days 00:00:00'],
dtype='timedelta64[ns]', freq='6H')
属性1:name
如果有的话,返回索引的名称
In [13]:
s1.name
Out[13]:
'Peter'
In [14]:
s4.name
Out[14]:
'Tom'
属性2:dtype
返回索引的数据类型
In [15]:
s1.dtype
Out[15]:
dtype('int64')
In [16]:
s8.dtype
Out[16]:
dtype('<M8[ns]')
In [17]:
s10.dtype
Out[17]:
dtype('<m8[ns]')
属性3:array
返回索引组成的数组:
In [18]:
s1.array
Out[18]:
<PandasArray>
[1, 2, 3, 4, 5, 6, 7]
Length: 7, dtype: int64
In [19]:
s5.array
Out[19]:
<PandasArray>
[1.5, 2.4, 3.7, 4.9]
Length: 4, dtype: float64
In [20]:
s8.array
Out[20]:
<DatetimeArray>
['2022-01-01 00:00:00', '2022-01-02 00:00:00', '2022-01-03 00:00:00',
'2022-01-04 00:00:00', '2022-01-05 00:00:00', '2022-01-06 00:00:00']
Length: 6, dtype: datetime64[ns]
属性4:shape
返回索引的形状:几行几列
In [21]:
s1.shape
Out[21]:
(7,)
In [22]:
s4.shape
Out[22]:
(4,)
In [23]:
s8.shape
Out[23]:
(6,)
属性5:size
返回索引的总个数:行数乘以列数
In [24]:
s1.size
Out[24]:
7
In [25]:
s2.size
Out[25]:
10
In [26]:
s5.size
Out[26]:
4
In [27]:
s10.size
Out[27]:
9
属性6:empty
返回索引是否为空
In [28]:
s1.empty
Out[28]:
False
In [29]:
s4.empty
Out[29]:
False
In [30]:
s10.empty
Out[30]:
False
属性7:ndim
返回索引的维度
In [31]:
s1.ndim
Out[31]:
1
In [32]:
s4.ndim
Out[32]:
1
属性8:T
将索引进行转置操作
In [33]:
s1.T
Out[33]:
Int64Index([1, 2, 3, 4, 5, 6, 7], dtype='int64', name='Peter')
In [34]:
s3.T
Out[34]:
Int64Index([1, 2, 3, 4, 5, 6, 7, 8], dtype='int64', name='Peter')
In [35]:
s6.T
Out[35]:
IntervalIndex([[0, 1), [1, 2), [2, 3), [3, 4), [4, 5), [5, 6)],
closed='left',
dtype='interval[int64]')
属性9:argmax
返回最大索引所在的位置
In [36]:
s1.argmax() # 最大索引所在的位置
Out[36]:
6
In [37]:
s5.argmax()
Out[37]:
3
属性10:is_integer
判断索引是否为整数型
In [38]:
s1.is_integer()
Out[38]:
True
In [39]:
s2.is_integer()
Out[39]:
True
In [40]:
s6.is_integer()
Out[40]:
False
属性汇总
对Pandas的常用属性进行一下简单的汇总。需要注意的是针对行索引的属性同样适用于列属性columns,因为它们二者都是同属于Pandas中的index对象。
[图片上传失败...(image-bf6cf4-1650039172740)]
网友评论