首页 > 其他 > 详细

DataFrame 索引和复合索引

时间:2019-12-03 13:32:32      阅读:335      评论:0      收藏:0      [点我收藏+]

前面按照多个条件进行分组产生的索引是复合索引

一、索引

# a、获取index
df.index
# b、指定index
df.index = []
# c、重新设置index
df.reindex([‘a‘, ‘b‘, ‘c‘])  
# 注意:一般不用
# d、指定某一列作为index
df2 = df1.set_index(‘O‘, drop=False)
# drop默认是True,丢弃指定的那一列
# e、指定某多列作为index
df2 = df1.set_index([‘M‘, ‘O‘], drop=False)
# f、对index进行去重操作 
df1.set_index(‘O‘, drop=False).index.unique()

二、复合索引

1、基础知识

# a、复合索引
df.set_index([‘c‘, ‘d‘])
# b、交换复合索引的顺序
df.swaplevel()

2、Series

# a、取Series
df.set_index([‘c‘, ‘d‘])[‘a‘]       # Series
# b、取具体值
df.set_index([‘c‘, ‘d‘])[‘a‘][‘c列的索引值‘][‘d列的索引值‘]
# 或
df.set_index([‘c‘, ‘d‘])[‘a‘][‘c列的索引值‘, ‘d列的索引值‘]

3、DataFrame

# a、取DataFrame
df.set_index([‘c‘, ‘d‘])[[‘a‘]]
# b、取具体值
df.set_index([‘c‘, ‘d‘])[[‘a‘]].loc[‘c列的索引值‘].loc[‘d列的索引值‘]

 

DataFrame 索引和复合索引

原文:https://www.cnblogs.com/wt7018/p/11976074.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!