我希望通过使用整数索引(使用iloc)来寻址行,并使用位置索引(使用loc)对列进行寻址来更改数据帧中的值。
无论如何要结合这两种方法吗?我相信它会是这样说的,我想要这个数据帧的第320行和标题为" columnTitle"的列。这可能吗?
答案 0 :(得分:1)
IIUC您可以直接在列上致电iloc
:
In [193]:
df = pd.DataFrame(columns=list('abc'), data = np.random.randn(5,3))
df
Out[193]:
a b c
0 -0.810747 0.898848 -0.374113
1 0.550121 0.934072 -1.117936
2 -2.113217 0.131204 -0.048545
3 1.674282 -0.611887 0.696550
4 -0.076561 0.331289 -0.238261
In [194]:
df['b'].iloc[3] = 0
df
Out[194]:
a b c
0 -0.810747 0.898848 -0.374113
1 0.550121 0.934072 -1.117936
2 -2.113217 0.131204 -0.048545
3 1.674282 0.000000 0.696550
4 -0.076561 0.331289 -0.238261
答案 1 :(得分:1)
ix
支持基于混合整数和标签的访问。
df = pd.DataFrame(np.random.randn(5,3), columns=list('ABC'))
>>> df
A B C
0 -0.473002 0.400249 0.332440
1 -1.291438 0.042443 0.001893
2 0.294902 0.927790 0.999090
3 1.415020 0.428405 -0.291283
4 -0.195136 -0.400629 0.079696
>>> df.ix[[0, 3, 4], ['B', 'C']]
B C
0 0.400249 0.332440
3 0.428405 -0.291283
4 -0.400629 0.079696
df.ix[[0, 3, 4], ['B', 'C']] = 0
>>> df
A B C
0 -0.473002 0.000000 0.000000
1 -1.291438 0.042443 0.001893
2 0.294902 0.927790 0.999090
3 1.415020 0.000000 0.000000
4 -0.195136 0.000000 0.000000