欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

对Pandas MultiIndex(多重索引)详解

程序员文章站 2024-02-08 23:21:52
创建多重索引 in [16]: df = pd.dataframe(np.random.randn(3, 8), index=['a', 'b', 'c'], c...

创建多重索引

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(多重索引)详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。