dataframe.at
时间: 2023-11-18 19:11:14 浏览: 29
`dataframe.at` is a method in Pandas library used to access and modify a specific cell in a DataFrame using its row and column labels. The syntax of the method is as follows:
```
dataframe.at[row_label, column_label]
```
where `row_label` and `column_label` are the labels of the row and column respectively. The method returns the value of the cell at the specified row and column.
For example, to access the cell at row 2 and column "Name" in a DataFrame `df`, we can use the following code:
```
value = df.at[2, "Name"]
```
Similarly, we can modify the value of the cell using the same syntax:
```
df.at[2, "Name"] = "John"
```
相关问题
dataframe.iloc
The iloc method in pandas is used to access the rows and columns of a dataframe using integer-based indexing.
Syntax: dataframe.iloc[row_index, column_index]
Example:
Consider the following dataframe:
```
import pandas as pd
data = {'name': ['John', 'Emily', 'Peter', 'Mary'],
'age': [25, 30, 35, 40],
'gender': ['M', 'F', 'M', 'F']}
df = pd.DataFrame(data)
```
Output:
```
name age gender
0 John 25 M
1 Emily 30 F
2 Peter 35 M
3 Mary 40 F
```
To access the first row of the dataframe, we can use iloc as follows:
```
df.iloc[0]
```
Output:
```
name John
age 25
gender M
Name: 0, dtype: object
```
To access the second column of the dataframe, we can use iloc as follows:
```
df.iloc[:, 1]
```
Output:
```
0 25
1 30
2 35
3 40
Name: age, dtype: int64
```
To access the value at the intersection of the second row and third column, we can use iloc as follows:
```
df.iloc[1, 2]
```
Output:
```
'F'
```
pandas.at用法
pandas.DataFrame.at方法是Pandas中用于访问和设置DataFrame中指定位置的值的方法。它的基本语法是df.at[row, column],其中row和column分别表示行标签和列标签。通过指定行标签和列标签,可以定位到DataFrame中具体的单元格,并对其进行读取或修改操作。
例如,可以使用以下代码示例来演示pandas.DataFrame.at方法的用法:
```python
import pandas as pd
# 创建一个示例DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
# 使用at方法读取特定单元格的值
value = df.at[1, 'B']
print(value) # 输出:5
# 使用at方法修改特定单元格的值
df.at[2, 'C'] = 10
print(df) # 输出修改后的DataFrame
```
在上面的示例中,我们首先创建了一个包含'A'、'B'和'C'三列的DataFrame,并使用at方法读取了第2行、'B'列的单元格的值(即5)。接下来,我们使用at方法修改了第3行、'C'列的单元格的值为10,并打印出修改后的DataFrame。