如何从 Pandas DataFrame 的单元格中获取值?
要从DataFrame的单元格中获取值,我们可以使用index和col变量。
步骤
创建二维、大小可变、潜在异构的表格数据df。
打印输入数据帧df。
初始化索引变量。
初始化col变量。
获取index和col变量对应的单元格值。
打印单元格值。
示例
import pandas as pd
df = pd.DataFrame(
{
"x": [5, 2, 1, 9],
"y": [4, 1, 5, 10],
"z": [4, 1, 5, 0]
}
)
print("Input DataFrame is:\n", df)
index = 2
col = "y"
cell_val = df.iloc[index][col]
print "单元格值在 ", index, "for column ", col, " : ", cell_val
index = 0
col = "x"
cell_val = df.iloc[index][col]
print "单元格值在 ", index, "for column ", col, " : ", cell_val
index = 1
col = "z"
cell_val = df.iloc[index][col]
print "单元格值在 ", index, "for column ", col, " : ", cell_val输出结果Input DataFrame is: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 单元格值在 2 for column y: 5 单元格值在 0 for column x: 5 单元格值在 1 for column z: 1
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短