_property _DataFrame.loc
Allow Inputs
- 单个标签,例如:“5”或“a”,“5”被解释为索引的标签
- 标签列表或数组,例如:[‘a’, ‘b’, ‘c’]
- 带有标签的切片对象,例如:’a’: ‘f’
- 与被切片的轴长度相同的布尔数组,例如:[True, False, True]
- 一个可对齐的布尔Series
- 一个可调用函数,带有一个参数并返回用于索引的有效输出
Example
```python import pandas as pd
df = pd.DataFrame({‘site’:[‘google’, ‘baidu’, ‘wiki’], ‘age’:[18, 39, 22], ‘price’: [1.0, 2.0, 3.0], ‘color’: [‘red’, ‘black’, None]},index=[‘first’,’second’,’third’])
df.loc[‘first’]
site google age 18 price 1 color red Name: first, dtype: object
<a name="U7UX2"></a>
# Example
```python
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc[['first', 'second']]
-----------------------------------------------------------
site age price color
first google 18 1.0 red
second baidu 39 2.0 black
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc['first', 'site']
-----------------------------------------------
'google'
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc['first':'third', 'site']
------------------------------------------
first google
second baidu
third wiki
Name: site, dtype: object
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc[[True, False, True]]
---------------------------------------------------
site age price color
first google 18 1.0 red
third wiki 22 3.0 None
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc[df['age'] > 18]
------------------------------------------------------
site age price color
second baidu 39 2.0 black
third wiki 22 3.0 None
Example
import pandas as pd
df = pd.DataFrame({"site":["google", "baidu", "wiki"],
"age":[18, 39, 22],
"price": [1.0, 2.0, 3.0],
"color": ["red", "black", None]},index=["first","second","third"])
df.loc[(df["age"] >= 18) & (df["age"] <= 25)]
----------------------------------------------------------
site age price color
first google 18 1.0 red
third wiki 22 3.0 None
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc[df['age'] > 18, ['age']]
-----------------------------------------------------
age
second 39
third 22
Example
对每一行应用函数,返回符合条件的行。
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc[lambda row: row['age'] == 18]
------------------------------------------------------
site age price color
first google 18 1.0 red
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc[['first', 'second'], ['age']] = 18
df
--------------------------------------------------
site age price color
first google 18 1.0 red
second baidu 18 2.0 black
third wiki 22 3.0 None
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc['first'] = 10
df
--------------------------------------------------------
site age price color
first 10 10 10.0 10
second baidu 39 2.0 black
third wiki 22 3.0 None
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc[:, 'age'] = 30
df
---------------------------------------------------
site age price color
first google 30 1.0 red
second baidu 30 2.0 black
third wiki 30 3.0 None
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=['first','second','third'])
df.loc[df['age'] > 18] = 0
df
-----------------------------------------------------------
site age price color
first google 18 1.0 red
second 0 0 0.0 0
third 0 0 0.0 0
Example
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki'],
'age':[18, 39, 22],
'price': [1.0, 2.0, 3.0],
'color': ['red', 'black', None]},index=[7, 8, 9])
df.loc[7:9]
------------------------------------------------------------
site age price color
7 google 18 1.0 red
8 baidu 39 2.0 black
9 wiki 22 3.0 None