data = pd.DataFrame({
'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}, index=['a', 'b', 'c'])
print(data)
輸出:
A B C
a 1 4 7
b 2 5 8
c 3 6 9
從輸出可以看出,index The element in is the row index name, The keys of the dictionary are the column index names,字典的值為DaraFrame的值,3行3列.
使用ioc屬性,依據行,Column index name to get the corresponding value.
print(data.loc['a', 'A']) 輸出:1
print(data.loc['a', 'C']) 輸出:7
Takes a range of values,Use a list to specify rows,列范圍
print(data.loc['a':'c', 'B']) # B列的值,because the line is specifieda到行c,B列
輸出:
a 4
b 5
c 6
Name: B, dtype: int64
同樣,No row index is specified,i.e. all lines,如下:
print(data.loc[:, 'B']) # 也是B列的值
Lines can be specified at the same time,列索引范圍
print(data.loc['a':'c', 'A':'B']) # A,B列的值
輸出:
A B
a 1 4
b 2 5
c 3 6
The row and column indices can be specified arbitrarily
print(data.loc['b':'c', 'A':'B']) # 第2,3 行 A,B列的值,
輸出:
A B
b 2 5
c 3 6
iloc()的使用,Values are based on row and column index numbers
print(data.iloc[1, 2]) # 第2行 第3列的數據, 索引默認從0開始
輸出: 8
Area values can also be taken,Get the index value of the row and column names in the range using the list
print(data.iloc[1:3, 0:3]) # 第2 行到第3行(開區間,不包含第4行),第1列 到第3The interval data for the column
輸出:
A B C
b 2 5 8
c 3 6 9
Every year at the beginning of
I learned it for a while pytho