当数值为Nan时,数据透视表边距将进行计数

时间:2019-09-30 16:05:19

标签: pivot-table nan margins

当值列表中有Nan时,我试图理解#len aggfunc。我认为table2的“总计”的列垂直边距应为9。但是它是7。为什么?

我尝试使用“ count”而不是len。此结果符合预期。

df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
                     "bar", "bar", "bar", "bar"]
               "B": ["one", "one", "one", "two", "two",
                     "one", "one", "two", "two"],
               "C": ["small", np.nan, "large", "small",
                     "small", "large", np.nan, "small", "large"],
               "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
               "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})

table1 = df.pivot_table(index = "A", columns = "B", values = "C", 
                        aggfunc = "count", margins = True)
table1["total"] = table1["one"] + table1["two"]
table1

table2 = df.pivot_table(index = "A", columns = "B", values = "C", 
                        aggfunc = len, margins = True)
table2["total"] = table2["one"] + table2["two"]
table2

enter image description here

enter image description here

0 个答案:

没有答案