How can I retrieve the value of a specific cell in pand…

You can use the .at[] or .iat[] method to access the value of a specific cell in a Pandas DataFrame.

  1. .at[] method: retrieve the value of a cell based on the label.
  2. .iat[] method: Retrieve the value of a cell based on its position.

Below is an example of using these two methods to retrieve the cell value:

import pandas as pd

# 创建DataFrame
data = {'Name': ['Tom', 'Nick', 'John'],
        'Age': [28, 32, 25],
        'City': ['London', 'New York', 'Paris']}
df = pd.DataFrame(data)

# 使用.at[]方法获取单元格的值
value_at = df.at[1, 'Age']
print('Value at (1, "Age"): ', value_at)

# 使用.iat[]方法获取单元格的值
value_iat = df.iat[2, 0]
print('Value at (2, 0): ', value_iat)

Output:

Value at (1, "Age"): 32
Value at (2, 0): John

In the above examples, .at[1, ‘Age’] retrieves the value of the ‘Age’ column in the second row, and .iat[2, 0] retrieves the value in the first column of the third row.

bannerAds