對Pandas MultiIndex(多重索引)詳解
創(chuàng)建多重索引
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對齊數(shù)據(jù)
數(shù)據(jù)準(zhǔn)備
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)索引的值,因?yàn)閟[:-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(多重索引)詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python GUI庫圖形界面開發(fā)之PyQt5打印控件QPrinter詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5打印控件QPrinter詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02
Python數(shù)值方法及數(shù)據(jù)可視化
這篇文章主要介紹了Python數(shù)值方法及數(shù)據(jù)可視化,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
使用Keras預(yù)訓(xùn)練模型ResNet50進(jìn)行圖像分類方式
這篇文章主要介紹了使用Keras預(yù)訓(xùn)練模型ResNet50進(jìn)行圖像分類方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
Python實(shí)現(xiàn)機(jī)器學(xué)習(xí)算法的分類
今天給大家整理了Python實(shí)現(xiàn)機(jī)器學(xué)習(xí)算法的分類的文章,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06
Pytorch中的variable, tensor與numpy相互轉(zhuǎn)化的方法
這篇文章主要介紹了Pytorch中的variable, tensor與numpy相互轉(zhuǎn)化的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

