python中ix用法_Python Pandas Series.ix用法及代码示例

本文介绍了Python Pandas库中的Series.ix属性,这是一个基于标签的索引器,用于根据标签获取Series中的值。通过示例展示了如何使用ix属性获取特定标签对应的值,如'City 4'和'Day 3'。
摘要由CSDN通过智能技术生成

Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。

Pandas 系列是带有轴标签的一维ndarray。标签不必是唯一的,但必须是可哈希的类型。该对象同时支持基于整数和基于标签的索引,并提供了许多方法来执行涉及索引的操作。

Pandas Series.ix属性主要是基于label-location的索引器,具有整数位置回退。它以标签为输入,并返回与该标签对应的值。

用法:Series.ix

参数:没有

返回:值

范例1:采用Series.ix属性以返回位于给定Series对象中指定标签处的值。

# importing pandas as pd

import pandas as pd

# Creating the Series

sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon'])

# Creating the row axis labels

sr.index = ['City 1', 'City 2', 'City 3', 'City 4']

# Print the series

print(sr)

输出:

6b51af085b22374a570e56f1b627dc82.png

现在我们将使用Series.ix属性以返回与“城市4”标签相对应的值。

# return the value

sr.ix['City 4']

输出:

77533ec4d44fdb1dd1a94cbb05dadccf.png

正如我们在输出中看到的,Series.ix属性已返回“里斯本”作为与给定Series对象中“城市4”标签相对应的值。

范例2:采用Series.ix属性以返回位于给定Series对象中指定标签处的值。

# importing pandas as pd

import pandas as pd

# Creating the Series

sr = pd.Series(['1/1/2018', '2/1/2018', '3/1/2018', '4/1/2018'])

# Creating the row axis labels

sr.index = ['Day 1', 'Day 2', 'Day 3', 'Day 4']

# Print the series

print(sr)

输出:

8db768338f375b3d3d48d397be1083ff.png

现在我们将使用Series.ix属性以返回与“第3天”标签相对应的值。

# return the value

sr.ix['Day 3']

输出:

cceaacf5fbbf44cb6053c80be1b27f71.png

正如我们在输出中看到的,Series.ix属性已返回“ 2018年3月1日”作为与给定Series对象中“第3天”标签对应的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值