这是我的代码:import pandas as pdimport numpyfrom quilt.data.bussiere import testar = numpy.array([[1.1, 2, 3.3, 4], [2.7, 10, 5.4, 7], [5.3, 9, 1.5, 15]])df = pd.DataFrame(ar, index = ['a1', 'a2', 'a3'], columns = ['A', 'B', 'C', 'D'])df.loc[df['A'] == 5.3] = df.loc[df['A'] == 2.7] df结果是一行 NaN : A B C Da1 1.1 2.0 3.3 4.0a2 2.7 10.0 5.4 7.0a3 NaN NaN NaN NaN如何正确更换?
1 回答
HUX布斯
TA贡献1876条经验 获得超6个赞
pandasdf.loc[df['A'] == 5.3]和df.loc[df['A'] == 2.7]are 都对索引敏感pandas object,因此index在您分配时会考虑到这一点,因为其中一个是索引是 a2,另一个是 a3,这就是您收到的原因NaN
df.loc[df['A'] == 5.3] = df.loc[df['A'] == 2.7] .values # using value here,without the index match assign
df
Out[137]:
A B C D
a1 1.1 2.0 3.3 4.0
a2 2.7 10.0 5.4 7.0
a3 2.7 10.0 5.4 7.0
添加回答
举报
0/150
提交
取消