Pandas DataFrame.loc 用於使用行和列標簽訪問或更新 DataFrame 的值。請注意,loc 是一個屬性,而不是函數 - 我們使用 [] 表示法提供參數。
允許的輸入如下:
- 
數字和字符串(例如 3、"a")。
- 
標簽列表(例如 [3,"a"])。
- 
切片對象(例如 "a":"d")。與標準 Python 切片不同,兩端都包含在內.
- 
一個布爾數組,其中將返回與 True對應的行/列。
- 
一個函數,將源 DataFrame 作為輸入並返回上述之一。 
注意
數字被視為標簽,因此它們始終被轉換為字符串。
返回值
如果結果是單行或單列,則返回Series。否則,返回DataFrame。
例子
訪問單個值
考慮以下 DataFrame :
df = pd.DataFrame({"A":[3,4],"B":[5,6]}, index=["a","b"])
df
   A  B
a  3  5
b  4  6要使用行和列標簽訪問 [bB] 處的值:
df.loc["b","B"]
6訪問行
考慮以下 DataFrame :
df = pd.DataFrame({"A":[3,4,5],"B":[6,7,8]}, index=["a","b","c"])
df
   A  B
a  3  6
b  4  7
c  5  8訪問單行
要獲取行b:
df.loc["b"]
A    4
B    7
Name: b, dtype: int64訪問多行
要訪問多行,請傳入行標簽列表,如下所示:
df.loc[["a","c"]]
   A  B
a  3  6
c  5  8您還可以使用切片語法,如下所示:
df.loc["a":"b"]
   A  B
a  3  6
b  4  7請注意,與 Python 的標準切片行為不同,兩端都包含在內.
訪問列
考慮以下 DataFrame :
df = pd.DataFrame({"A":[3,4],"B":[5,6]}, index=["a","b"])
df
   A  B
a  3  5
b  4  6訪問單個列
要訪問單個列:
df.loc[:,"A"]
a    3
b    4
Name: A, dtype: int64這裏,逗號之前的:表示我們要檢索所有行。逗號後麵的 "A" 表示我們隻想獲取 A 列。
訪問多列
要訪問多個列,隻需在逗號後麵傳入列標簽列表即可:
df.loc[:,["A","B"]]
   A  B
a  3  5
b  4  6訪問行和列
要訪問特定的行和列,隻需組合上述訪問模式即可。
例如,考慮以下 DataFrame:
df = pd.DataFrame({"A":[3,4],"B":[5,6],"C":[7,8]}, index=["a","b"])
df
   A  B  C
a  3  5  7
b  4  6  8要獲取行 a 、列 A 和 B 中的數據:
df.loc["a", ["A","B"]]
A    3
B    5
Name: a, dtype: int64使用函數
loc 屬性還允許您傳遞函數。
有條件選擇行
考慮以下 DataFrame :
df = pd.DataFrame({"A":[3,4],"B":[5,6]})
df
   A  B
0  3  5
1  4  6我們首先定義一個匹配條件:
def criteria(my_df):
 return my_df["A"] + my_df["B"] > 9該函數接受源 DataFrame 作為參數,並返回布爾值 Series 來指示是否滿足條件。因此,我們的 criteria 函數將用於選擇值總和大於 9 的行:
my_df["A"] + my_df["B"] > 9
0    False
1     True
dtype: bool我們可以將我們的標準直接傳遞到 loc 中,如下所示:
df.loc[criteria]
   A  B
1  4  6正如您所期望的,我們還可以指定要包含的列:
df.loc[criteria, "A"]
1    4
Name: A, dtype: int64使用布爾掩碼
考慮以下 DataFrame :
df = pd.DataFrame({"A":[3,4,5],"B":[6,7,8]})
df
   A  B
0  3  6
1  4  7
2  5  8我們可以使用布爾掩碼(即布爾值列表)來提取某些行/列:
df.loc[[True,False,True]]
   A  B
0  3  6
2  5  8請注意如何僅返回與 True 對應的行。
複製與查看
根據上下文,loc 可以返回 view 或 copy 。不幸的是,返回的規則很複雜,因此最佳實踐是使用 _is_view 屬性親自檢查這一點。
有一條規則很容易記住——loc返回看法提取單列時的數據:
df = pd.DataFrame({"A":[2,3], "B":[4,5]})
col_A = df.loc[:,"A"]
col_A._is_view
True由於 col_A 是一個視圖,因此修改 col_A 將會改變原始的 df 。
更新值
考慮以下 DataFrame :
df = pd.DataFrame({"A":[3,4], "B":[5,6]}, index=["a","b"])
df
   A  B
0  3  5
1  4  6更新單個值
要更改 0 行 B 列的值:
df.loc["a","B"] = 9
df
   A  B
a  3  9
b  4  6更新多個值
要更新多個值,隻需使用上述任何訪問模式,然後使用 = 分配新值。
例如,要更新第一行:
df.loc["a",["A","B"]] = [8,9]
df
   A  B
a  8  9
b  4  6相關用法
- Python Pandas DataFrame lookup方法用法及代碼示例
- Python Pandas DataFrame lt方法用法及代碼示例
- Python Pandas DataFrame last_valid_index方法用法及代碼示例
- Python PySpark DataFrame limit方法用法及代碼示例
- Python Pandas DataFrame le方法用法及代碼示例
- Python Pandas DataFrame last方法用法及代碼示例
- Python Pandas DataFrame empty屬性用法及代碼示例
- Python Pandas DataFrame pop方法用法及代碼示例
- Python Pandas DataFrame nsmallest方法用法及代碼示例
- Python Pandas DataFrame sample方法用法及代碼示例
- Python Pandas DataFrame items方法用法及代碼示例
- Python Pandas DataFrame max方法用法及代碼示例
- Python Pandas DataFrame swaplevel方法用法及代碼示例
- Python Pandas DataFrame agg方法用法及代碼示例
- Python Pandas DataFrame copy方法用法及代碼示例
- Python Pandas DataFrame pow方法用法及代碼示例
- Python Pandas DataFrame insert方法用法及代碼示例
- Python Pandas DataFrame all方法用法及代碼示例
- Python Pandas DataFrame unstack方法用法及代碼示例
- Python Pandas DataFrame mean方法用法及代碼示例
- Python PySpark DataFrame filter方法用法及代碼示例
- Python Pandas DataFrame tz_convert方法用法及代碼示例
- Python Pandas DataFrame isin方法用法及代碼示例
- Python PySpark DataFrame collect方法用法及代碼示例
- Python PySpark DataFrame intersect方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas DataFrame | loc property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
