How can pandas retrieve data from a specific cell?
To access data from a specific cell, you can use either the “loc” or “at” method.
Using the loc method:
import pandas as pd
# 创建一个示例DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
# 使用loc方法读取指定单元格的数据
value = df.loc[1, 'B']
print(value)
Utilize the “at” method:
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)
The above code will output 5, indicating that it is reading the data from the cell in the second row and second column (using 0-based indexing).