df.loc [rows,[col]] vs df.loc [rows,col]赋值

时间:2017-05-16 22:33:31

标签: pandas

以下分配的行为有何不同?

df.loc[rows, [col]] = ...
df.loc[rows, col] = ...

例如:

r = pd.DataFrame({"response": [1,1,1],},index = [1,2,3] )
df = pd.DataFrame({"x": [999,99,9],}, index = [3,4,5] )
df = pd.merge(df, r, how="left", left_index=True, right_index=True)

df.loc[df["response"].isnull(), "response"] = 0
print df
     x  response
3  999       0.0
4   99       0.0
5    9       0.0

df.loc[df["response"].isnull(), ["response"]] = 0
print df
     x  response
3  999       1.0
4   99       0.0
5    9       0.0

为什么我认为第一个与第二个表现不同?

1 个答案:

答案 0 :(得分:4)

df.loc[df["response"].isnull(), ["response"]]

返回一个DataFrame,因此如果要为其指定内容,则必须通过索引和列

进行对齐

演示:

In [79]: df.loc[df["response"].isnull(), ["response"]] = \
             pd.DataFrame([11,12], columns=['response'], index=[4,5])

In [80]: df
Out[80]:
     x  response
3  999       1.0
4   99      11.0
5    9      12.0

或者你可以指定一个相同形状的数组/矩阵:

In [83]: df.loc[df["response"].isnull(), ["response"]] = [11, 12]

In [84]: df
Out[84]:
     x  response
3  999       1.0
4   99      11.0
5    9      12.0

我还考虑使用fillna()方法:

In [88]: df.response = df.response.fillna(0)

In [89]: df
Out[89]:
     x  response
3  999       1.0
4   99       0.0
5    9       0.0