当前位置: 移动技术网 > IT编程>脚本编程>Python > 对Pandas MultiIndex(多重索引)详解

对Pandas MultiIndex(多重索引)详解

2018年11月30日  | 移动技术网IT编程  | 我要评论

陈树隆后台,宝宝图片背景,小时代3 票房

创建多重索引

in [16]: df = pd.dataframe(np.random.randn(3, 8), index=['a', 'b', 'c'], columns=index)

in [17]: df
out[17]: 
first  bar     baz     foo     qux \
second  one  two  one  two  one  two  one 
a  0.895717 0.805244 -1.206412 2.565646 1.431256 1.340309 -1.170299 
b  0.410835 0.813850 0.132003 -0.827317 -0.076467 -1.187678 1.130127 
c  -1.413681 1.607920 1.024180 0.569605 0.875906 -2.211372 0.974466 

first    
second  two 
a  -0.226169 
b  -1.436737 
c  -2.006747 

获得索引信息

get_level_values

in [23]: index.get_level_values(0)
out[23]: index(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], dtype='object', name='first')

in [24]: index.get_level_values('second')
out[24]: index(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'], dtype='object', name='second')

基本索引

in [25]: df['bar']
out[25]: 
second  one  two
a  0.895717 0.805244
b  0.410835 0.813850
c  -1.413681 1.607920

in [26]: df['bar', 'one']
out[26]: 
a 0.895717
b 0.410835
c -1.413681
name: (bar, one), dtype: float64

in [27]: df['bar']['one']
out[27]: 
a 0.895717
b 0.410835
c -1.413681
name: one, dtype: float64

使用reindex对齐数据

数据准备

in [11]: s = pd.series(np.random.randn(8), index=arrays)

in [12]: s
out[12]: 
bar one -0.861849
  two -2.104569
baz one -0.494929
  two 1.071804
foo one 0.721555
  two -0.706771
qux one -1.039575
  two 0.271860
dtype: float64

s序列加(0~-2)索引的值,因为s[:-2]没有最后两个的索引,所以为nan.s[::2]意思是步长为1.

in [34]: s + s[:-2]
out[34]: 
bar one -1.723698
  two -4.209138
baz one -0.989859
  two 2.143608
foo one 1.443110
  two -1.413542
qux one   nan
  two   nan
dtype: float64

in [35]: s + s[::2]
out[35]: 
bar one -1.723698
  two   nan
baz one -0.989859
  two   nan
foo one 1.443110
  two   nan
qux one -2.079150
  two   nan
dtype: float64

以上这篇对pandas multiindex(多重索引)详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网