at()根据行列的名称,查询对应值。
iat()根据行列的位置编号,查询对应的值
at()函数
**
待读取文件预览:
代码:
import pandas as pd
# 读取excel
file_path = r'C:\Users\123\Desktop\pandastest\example.xlsx'
df = pd.read_excel(file_path, sheet_name=0, header=0, skipfooter=0)
# 预览前5行
print(df.head(5))
# 获取index = 2,column =“编号”的值
x = df.at[2, '编号']
print(x)
#修改index = 2,column =“编号”的值
df.at[2, '编号'] = 9999999
# 获取index = 2,column =“编号”的值
print(df.at[2, '编号'])
# loc函数,获取index = 2的行。
print(df.loc[2].at['编号'])
运行结果:
*
iat()函数
import pandas as pd
# 读取excel
file_path = r'C:\Users\123\Desktop\pandastest\example.xlsx'
df = pd.read_excel(file_path, sheet_name=0, header=0, skipfooter=0)
# 预览前5行
print(df.head(5))
# 通过位置编号查询值。查询row序号1,column序号为2的值
x = df.iat[1, 2]
print(x)
# 通过位置编号修改值。查询row序号1,column序号为2的值
df.iat[1, 2] = 9999999
# 获取index = 2,column =“编号”的值
print(df.iat[1, 2])
# loc函数,获取index = 2的行。
print(df.iloc[1].iat[2])
运行结果: