这是我的代码:
import pandas as pd
import numpy
from quilt.data.bussiere import test
ar = 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 D
a1 1.1 2.0 3.3 4.0
a2 2.7 10.0 5.4 7.0
a3 NaN NaN NaN NaN
如何正确更换?
答案 0 :(得分:2)
pandas
是索引敏感的df.loc[df['A'] == 5.3]
和df.loc[df['A'] == 2.7]
都是pandas
object
,因此在您进行操作时将考虑index
Assign,因为其中一个是索引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