DataFrame 索引和复合索引

时间:2019-12-03 13:32:32   收藏:0   阅读:329

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

一、索引

# 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列的索引值]

 

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!