dataframe.loc[]
时间: 2023-11-18 22:57:47 浏览: 67
浅谈pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)
`dataframe.loc[]` is a method used to access a group of rows and columns in a pandas DataFrame. It allows selection of rows and columns by label or a boolean array.
For example, if we have a DataFrame called `df` and we want to select rows with label 'A' and columns 'X' and 'Y', we can use the following code:
```
df.loc['A', ['X', 'Y']]
```
This will return a new DataFrame with the rows and columns specified.
Alternatively, we can use boolean indexing with the `loc` method to select rows based on a condition. For example, if we want to select all rows where the value in column 'X' is greater than 10, we can use the following code:
```
df.loc[df['X'] > 10]
```
This will return a new DataFrame with only the rows where the condition is True.
阅读全文