溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

pandas中層次索引與取值的示例分析

發(fā)布時間:2021-07-26 11:32:25 來源:億速云 閱讀:322 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下pandas中層次索引與取值的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1、層次索引

1.1 定義

在某一個方向擁有多個(兩個及兩個以上)索引級別,就叫做層次索引。

通過層次化索引,pandas能夠以較低維度形式處理高緯度的數(shù)據(jù)

通過層次化索引,可以按照層次統(tǒng)計數(shù)據(jù)

層次索引包括Series層次索引和DataFrame層次索引

1.2 Series的層次索引

import numpy as np
import pandas as pd

s1 = pd.Series(data=[99, 80, 76, 80, 99],
    index=[['2017', '2017', '2018', '2018', '2018'], ['張伊曼', '張巧玲', '張詩詩', '張思思', '張可可']])
print(s1)

pandas中層次索引與取值的示例分析

1.3 DataFrame的層次索引

# DataFrame的層次索引
df1 = pd.DataFrame({
 'year': [2016, 2016, 2017, 2017, 2018],
 'fruit': ['apple', 'banana', 'apple', 'banana', 'apple'],
 'production': [10, 30, 20, 70, 100],
 'profits': [40, 30, 60, 80,10],
})
print("df1===================================")
print(df1)

df2 = df1.set_index(['year', 'fruit'])
print("df2===================================")
print(df2)

print("df2.index===================================")
print(df2.index)

print("df2.sum(level='year')===================================")
print(df2.sum(level='year'))

print("df2.mean(level='fruit')===================================")
print(df2.mean(level='fruit'))

print("df2.sum(level=['year', 'fruit'])===================================")
print(df2.sum(level=['year', 'fruit']))

pandas中層次索引與取值的示例分析

pandas中層次索引與取值的示例分析

2、取值的新方法

ix是比較老的方法 新方式是使用iloc loc

iloc 對下標(biāo)值進行操作 Series與DataFrame都可以操作

loc 對索引值進行操作 Series與DataFrame都可以操作

2.1 Series

# # 取值的新方法
s1 = pd.Series(data=[99, 80, 76, 80, 99],
    index=[['2017', '2017', '2018', '2018', '2018'], ['張伊曼', '張巧玲', '張詩詩', '張思思', '張可可']])

print("s1=================================")
print(s1)

print("s1.iloc[2]=================================")
print(s1.iloc[2])

print("s1.loc['2018']['張思思']=================================")
print(s1.loc['2018']['張思思'])

pandas中層次索引與取值的示例分析

2.2 DataFrame

df1 = pd.DataFrame({
 'year': [2016, 2016, 2017, 2017, 2018],
 'fruit': ['apple', 'banana', 'apple', 'banana', 'apple'],
 'production': [10, 30, 20, 70, 100],
 'profits': [40, 30, 60, 80,10],
})
print("df1===================================")
print(df1)

print("舊方法獲取值===================================")
print("df1['year'][0]===================================")
print(df1['year'][0])

print("df1.ix[0]['year']===================================")
print(df1.ix[0]['year'])

print("新方法獲取值===================================")
print("df1.iloc[0][3]===================================")
print(df1.iloc[0][3])

print("df1.loc[0]['year']===================================")
print(df1.loc[0]['year'])

pandas中層次索引與取值的示例分析

以上是“pandas中層次索引與取值的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI