1、loc
通過行標籤索引行資料
(1)、loc[『d』]:獲取第』d』行資料
import pandas as pd
data = [[1,2,3],[4,5,6]]
index = [『d』,'e']
columns=['a','b','c']
df = pd.dataframe(data=data, index=index, columns=columns)
print(df.loc['e'])
a 4
b 5
c 6
name: 1, dtype: int64
(2)、loc['d':]
獲取第『d』行及之後的多行資料
import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.dataframe(data=data, index=index, columns=columns)
print(df.loc['d':])
a b c
d 1 2 3
e 4 5 6
(3)、
loc['d',['b']]索引第『d』行第『b』列
import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.dataframe(data=data, index=index, columns=columns)
print(df.loc['d',['b']])
b 2
name: d, dtype: int64
通過df.[列標籤]可直接獲取某列資料,但當標籤未知時可通過這種方式獲取列資料
2、iloc
通過行號獲取行資料
(1)、iloc[1]獲取第1行資料
import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.dataframe(data=data, index=index, columns=columns)
print(df.iloc[1])
a 4
b 5
c 6
name: e, dtype: int64
(2)、iloc[0:]
獲取第0行及之後的多行資料
import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.dataframe(data=data, index=index, columns=columns)
print(df.iloc[0:])
a b c
d 1 2 3
e 4 5 6
(3)、iloc[:,[1]]
獲取第1列資料
import pandas as pd
data = [[1,2,3],[4,5,6]]
index = ['d','e']
columns=['a','b','c']
df = pd.dataframe(data=data, index=index, columns=columns)
print(df.iloc[:,[1]])
b
d 2
e 5
3、ix
前兩種的混合索引,python3已經不使用這種索引方式。
pandas資料選擇(索引)
import pandas as pd import numpy as npdates pd.date range 20180101 periods 6 df pd.dataframe np.arange 24 reshape 6,4 index dates,columns a b c d prin...
pandas資料新索引 reindex
dataframe.reindex labels none,index none,columns none,axis none,method none,copy true,level none,fill value nan,limit none,tolerance none 常用關鍵引數 metho...
pandas資料的索引操作
coding utf 8 series索引 行索引 import pandas as pd import numpy as np ser obj pd.series range 5 index a b c d e print ser obj 行索引獲單個值 print ser obj b ser o...