Python--pandas-數(shù)據(jù)選取loc、iloc、ix函數(shù)

基礎(chǔ)概述

loc:通過行標簽索引數(shù)據(jù),例如取index為a的行;location的縮寫
iloc:通過行號索引行數(shù)據(jù),例如取第2行數(shù)據(jù);Integer and location的縮寫
ix:通過行標簽或行號索引數(shù)據(jù)(基于loc和iloc的混合);

基礎(chǔ)使用用法

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2019-06-05 22:59
# @Author  : LiYahui
# @Description : 選取部分的數(shù)據(jù)
import pandas as pd
# 嵌套的嵌套序列數(shù)據(jù)結(jié)構(gòu)
city_data = {'城市': pd.Series(['北京', '上海', '深圳', '成都', '杭州'], index=['a', 'b', 'c', 'd', 'e']),
             '人口/千萬': pd.Series([2171, 2415, 1191, 901,899], index=['a', 'b', 'c', 'd', 'e']),
             '年份': pd.Series([2015, 2016, 2015, 2016, 2015], index=['a', 'b', 'c', 'd', 'e'])
             }
df1 = pd.DataFrame(city_data)
# print(df1)
'''
   城市  人口/千萬    年份
a  北京   2171  2015
b  上海   2415  2016
c  深圳   1191  2015
d  成都    901  2016
e  杭州    899  2015
'''

利用loc、iloc提取行數(shù)據(jù)

提取單行數(shù)據(jù)

# 利用loc、iloc提取行數(shù)據(jù)
# 取索引為a的行
df2=df1.loc['a']
# print(df2)
'''
城市         北京
人口/千萬    2171
年份       2015
Name: a, dtype: object
'''

#取第一行數(shù)據(jù),索引為'a'的行就是第一行,所以結(jié)果相同
df3=df1.iloc[0]
# print(df3)
'''
城市         北京
人口/千萬    2171
年份       2015
Name: a, dtype: object
'''
# 對比發(fā)現(xiàn),df2和df3是完全一樣的;

提取多行數(shù)據(jù)

# 取索引為a,c的行,索引必須放入一個list中
df2=df1.loc[['a','c']]
print(df2)
'''
   城市  人口/千萬    年份
a  北京   2171  2015
c  深圳   1191  2015
'''

#取第一行、第三行數(shù)據(jù),索引為'a'的行就是第一行,c就是第三行,所以結(jié)果相同
df3=df1.iloc[[0,2]]
print(df3)
'''
   城市  人口/千萬    年份
a  北京   2171  2015
c  深圳   1191  2015
'''
# 對比發(fā)現(xiàn),df2和df3是完全一樣的;

利用loc、iloc提取列數(shù)據(jù)


df4=df1.loc[:,['城市']] # 取a列所有行,多取幾列格式為 data.loc[:,['a','c','f']]
df6=df1.loc[:,['城市','年份']] # 取a列所有行,多取幾列格式為 data.loc[:,['a','c','f']]
print(df4)
'''
   城市
a  北京
b  上海
c  深圳
d  成都
e  杭州
'''
print(df6)
'''
   城市    年份
a  北京  2015
b  上海  2016
c  深圳  2015
d  成都  2016
e  杭州  2015
'''
df5=df1.iloc[:,[0]] # 取第0列所有行,多取幾列格式為 data.iloc[:,[0,2,5]]
print(df5)
'''
   城市
a  北京
b  上海
c  深圳
d  成都
e  杭州
'''

利用loc、iloc提取指定行、指定列數(shù)據(jù)

df7=df1.loc[['a','c'],['城市','年份']] #提取index為a、c,column為'城市','年份'的數(shù)據(jù)
print(df7)
'''
   城市    年份
a  北京  2015
d  成都  2016
'''
df8=df1.iloc[[0,3],[0,2]] # 提取第0、3行,第0、2列數(shù)據(jù)
print(df8)
'''
   城市    年份
a  北京  2015
d  成都  2016
'''

利用loc、iloc提取所有數(shù)據(jù)

df9=df1.loc[:,:]
print(df9)
'''
   城市  人口/千萬    年份
a  北京   2171  2015
b  上海   2415  2016
c  深圳   1191  2015
d  成都    901  2016
e  杭州    899  2015
'''
df10=df1.iloc[:,:]
print(df10)
'''
   城市  人口/千萬    年份
a  北京   2171  2015
b  上海   2415  2016
c  深圳   1191  2015
d  成都    901  2016
e  杭州    899  2015
'''

利用loc函數(shù),根據(jù)某個數(shù)據(jù)來提取數(shù)據(jù)所在的行

df11=df1.loc[df1["城市"]=="杭州"] ##提取df1數(shù)據(jù)(篩選條件: 城市列中值為杭州所在的行數(shù)據(jù))
print(df11)
'''
   城市  人口/千萬    年份
e  杭州    899  2015
'''

利用loc函數(shù),取指定坐標系的元素值

# 例如:取第a行,列名為=年份的元素
ele=df["a","年份"]

ix用法

ix的操作比較復(fù)雜,在pandas版本0.20.0及其以后版本中,ix已經(jīng)不被推薦使用,建議采用iloc和loc實現(xiàn)ix。
推薦學習博客:Python: pandas中ix的詳細講解

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容