pandas建立多重索引的幾種方式

2021-10-17 01:45:32 字數 3376 閱讀 1894

使用pandas建立多重索引的方式有很多,下面舉出常見的幾種:

1、pd.multiindex.from_tuples方法

>>

>

import pandas as pd

>>

>

import numpy as np

>>

> arrays =[[

"bar"

,"bar"

,"baz"

,"baz"

,"foo"

,"foo"

,"qux"

,"qux"],

["one"

,"two"

,"one"

,"two"

,"one"

,"two"

,"one"

,"two"]]

>>

> tuples =

list

(zip

(*arrays)

)>>

> index = pd.multiindex.from_tuples(tuples)

>>

> index

multiindex([(

'bar'

,'one'),

('bar'

,'two'),

('baz'

,'one'),

('baz'

,'two'),

('foo'

,'one'),

('foo'

,'two'),

('qux'

,'one'),

('qux'

,'two')]

,)>>

> s = pd.series(np.random.randn(8)

, index=index)

>>

> s

bar one 0.612171

two -

0.615973

baz one -

1.611292

two -

0.708034

foo one -

1.588981

two -

0.998916

qux one -

0.320857

two -

1.623501

dtype: float64

2、pd.multiindex.from_product

#該方法與from_tuple的是有區別,具體一看例子就懂

>>

> iterables =[[

"bar"

,"baz"

,"foo"

,"qux"],

["one"

,"two"]]

>>

> pd.multiindex.from_product(iterables, names=

["first"

,"second"])

multiindex([(

'bar'

,'one'),

('bar'

,'two'),

('baz'

,'one'),

('baz'

,'two'),

('foo'

,'one'),

('foo'

,'two'),

('qux'

,'one'),

('qux'

,'two')]

, names=

['first'

,'second'

])

3、pd.multiindex.from_frame直接使用已有dataframe新增

>>

> df = pd.dataframe([[

"bar"

,"one"],

["bar"

,"two"],

["foo"

,"one"],

["foo"

,"two"]]

,columns=

["first"

,"second"])

>>

> df

first second

0 bar one

1 bar two

2 foo one

3 foo two

>>

> pd.multiindex.from_frame(df)

multiindex([(

'bar'

,'one'),

('bar'

,'two'),

('foo'

,'one'),

('foo'

,'two')]

, names=

['first'

,'second'

])

4、直接通過構建arrays

>>

> arrays =

[np.array(

["bar"

,"bar"

,"baz"

,"baz"

,"foo"

,"foo"

,"qux"

,"qux"])

,np.array(

["one"

,"two"

,"one"

,"two"

,"one"

,"two"

,"one"

,"two"])

]>>

> s = pd.series(np.random.randn(8)

, index=arrays)

>>

> s

bar one 0.953744

two 0.069687

baz one -

0.205349

two 1.093807

foo one -

0.845178

two -

1.040949

qux one 1.646463

two -

0.244347

dtype: float64

哈哈,以上就是構建multiindex的幾種常見方式,歡迎關注python小工具,一起學習python和pandas。

pandas中建立多級索引的方法

1 方法一 使用pandas.multiindex.from arrays in 25 pd.multiindex.from arrays a a b b 1,2,1,2 out 25 multiindex levels a b 1,2 labels 0,0,1,1 0,1,0,1 上例中,多級索引...

pandas 01 Series 的幾種建立方法

pandas.series 的幾種建立方法。import numpy as np import pandas as pd 使用乙個列表生成乙個series s1 pd.series 1,2,3,4 print s1 0 1 1 2 2 3 3 4 dtype int64 返回所有的索引 print ...

mysql幾種索引 mysql的幾種索引

為什麼建立索引後,速度就會變快?答 使用索引後,查詢是按二叉樹演算法來查詢到記錄 索引使用的注意事項 索引的代價 1.占用磁碟空間 2.對dml 新增 修改 刪除 操作有影響,變慢 在哪些列上適合新增索引 1.較頻繁的作為查詢條件字段應該建立索引 select from emp where empn...