pandas带有重复索引操作方法
作者:修炼之路 时间:2023-04-19 20:22:12
有的时候,可能会遇到表格中出现重复的索引,在操作重复索引的时候可能要注意一些问题。
一、判断索引是否重复
a、Series索引重复判断
s = Series([1,2,3,4,5],index=["a","a","b","b","c"])
print(s.index.is_unique)
#False
Series.index.is_unique为False表示索引重复。
b、DataFrame索引重复判断
a = np.arange(9).reshape(3,3)
data = DataFrame(a,index=["a","b","c"],columns=["one","two","one"])
#判断行索引是否重复
print(data.index.is_unique)
#True
#判断列索引是否重复
print(data.columns.is_unique)
#False
二、索引取值
如果一个索引对应多个值,Series返回的是一个Series。如果一个索引对应一个值的时候,Series返回的是一个标量,DataFrame返回的是始终是一个DataFrame。
a、Series的索引取值
s = Series([1, 2, 3, 4, 5], index=["a", "a", "b", "b", "c"])
print(type(s["a"]))
#<class 'pandas.core.series.Series'>
print(s["a"])
'''
a 1
a 2
'''
#选取第一个a
print(s[:1])
#a 1
print(s[[0]])
#a 1
b、DataFrame的索引取值
a = np.arange(9).reshape(3,3)
data = DataFrame(a,index=["a","b","b"],columns=["one","two","one"])
#对行进行选取
print(type(data.ix["b"]))
#<class 'pandas.core.frame.DataFrame'>
print(data.ix["b"])#与data.xs("b")等价
'''
one two one
b 3 4 5
b 6 7 8
'''
#选取第二行
print(type(data.ix[1:2]))#与data[1:2]等价
#<class 'pandas.core.frame.DataFrame'>
print(data.ix[1:2])
#b 3 4 5
print(data.ix[[1]])
#b 3 4 5
#对列进行选取
print(data["one"])#等价于data.one 或 data.xs("one",axis=1)
'''
one one
a 0 2
b 3 5
b 6 8
'''
#选取第一列
print(data.ix[:,0])
'''
a 0
b 3
b 6
'''
print(data.ix[:,:1])
'''
one
a 0
b 3
b 6
'''
来源:https://blog.csdn.net/sinat_29957455/article/details/78994764
标签:pandas,重复,索引
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
文案是网站的性格
2009-09-02 13:05:00
![](https://img.aspxhome.com/file/UploadPic/20099/2/01-84s.jpg)
Python中xlsx文件转置操作详解(行转列和列转行)
2022-02-18 03:36:34
![](https://img.aspxhome.com/file/2023/0/92310_0s.jpg)
详解MySQL如何有效的存储IP地址及字符串IP和数值之间如何转换
2024-01-28 11:10:41
python实现与redis交互操作详解
2022-07-07 17:37:18
![](https://img.aspxhome.com/file/2023/0/130620_0s.png)
Vue2.0实现1.0的搜索过滤器功能实例代码
2024-05-09 10:40:30
微软建议的ASP性能优化28条守则(9)
2005-05-30 16:05:00
关于vue3中setup函数的使用
2024-06-05 09:16:16
![](https://img.aspxhome.com/file/2023/9/123059_0s.jpg)
网页设计五原则
2007-11-03 13:50:00
Postman返回中文乱码的解决方案
2022-03-16 15:03:02
![](https://img.aspxhome.com/file/2023/8/132378_0s.png)
MySQL 回表,覆盖索引,索引下推
2024-01-21 12:56:59
![](https://img.aspxhome.com/file/2023/9/118189_0s.png)
asp更改Windows2000管理者密码?
2010-06-26 11:03:00
在Python编程过程中用单元测试法调试代码的介绍
2023-12-10 02:16:46
python实现对doc,txt,xls文档的读写操作
2021-05-09 20:01:14
JavaScript二维数组实现的省市联动菜单
2024-06-14 22:02:56
Django展示可视化图表的多种方式
2022-07-31 07:15:53
![](https://img.aspxhome.com/file/2023/1/118511_0s.png)
详解python之协程gevent模块
2022-12-20 05:42:54
python3安装pip3(install pip3 for python 3.x)
2023-03-30 07:19:09
![](https://img.aspxhome.com/file/2023/6/75286_0s.png)
django实现模板中的字符串文字和自动转义
2023-10-18 16:49:46
Go应该如何实现二级缓存
2024-02-19 17:07:53
在python中使用requests 模拟浏览器发送请求数据的方法
2022-05-05 03:17:35