_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```pythonimport pandas as pddf = 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 colorfirst google 18 1.0 redsecond baidu 39 2.0 black
Example
import pandas as pddf = 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 pddf = 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 googlesecond baiduthird wikiName: site, dtype: object
Example
import pandas as pddf = 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 colorfirst google 18 1.0 redthird wiki 22 3.0 None
Example
import pandas as pddf = 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 colorsecond baidu 39 2.0 blackthird wiki 22 3.0 None
Example
import pandas as pddf = 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 colorfirst google 18 1.0 redthird wiki 22 3.0 None
Example
import pandas as pddf = 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']]-----------------------------------------------------agesecond 39third 22
Example
对每一行应用函数,返回符合条件的行。
import pandas as pddf = 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 colorfirst google 18 1.0 red
Example
import pandas as pddf = 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']] = 18df--------------------------------------------------site age price colorfirst google 18 1.0 redsecond baidu 18 2.0 blackthird wiki 22 3.0 None
Example
import pandas as pddf = 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'] = 10df--------------------------------------------------------site age price colorfirst 10 10 10.0 10second baidu 39 2.0 blackthird wiki 22 3.0 None
Example
import pandas as pddf = 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'] = 30df---------------------------------------------------site age price colorfirst google 30 1.0 redsecond baidu 30 2.0 blackthird wiki 30 3.0 None
Example
import pandas as pddf = 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] = 0df-----------------------------------------------------------site age price colorfirst google 18 1.0 redsecond 0 0 0.0 0third 0 0 0.0 0
Example
import pandas as pddf = 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 color7 google 18 1.0 red8 baidu 39 2.0 black9 wiki 22 3.0 None
