两个选择都会产生相同的结果,但有一个我无法为DataFrame赋值

时间:2014-02-07 17:27:47

标签: python numpy pandas variable-assignment

请考虑这个例子:

import numpy as np
import pandas as pd
df = pd.DataFrame([1,2,3,4,5], columns=["col"])

df[df["col"] == 3]["col"] = 11 # Does not work.
df["col"][df["col"] == 3] = 55 # Does work!

虽然分配的结果不同,但基础选择会产生相同的结果:

df[df["col"] == 3]["col"] # Looks like the same as
df["col"][df["col"] == 3] # this

为什么一种方式有效,另一种方式无效?

0 个答案:

没有答案