如何保留NaN而不是在数据透视表中填充零?

时间:2019-06-24 18:39:02

标签: python pandas

这是我要透视的表:

data = [[1,1,"red", 59, np.nan],[1,1,"blue", "yes", np.nan],[1,2,"red", 

8.6, np.nan],[1,2,"blue", "other", np.nan],
[2,1,"red", 61.1, np.nan], [2,1,"blue", "response", np.nan],[2,2,"red", 60, np.nan],[2,2,"blue", 63, np.nan],
[3,1,"red", np.nan, 2],[3,1,"blue", np.nan, 2],[3,2,"red", np.nan,2],[3,2,"blue", np.nan,2],
[4,1,"red", 0, np.nan],[4,1,"blue", 0, np.nan],[4,2,"red", 0,np.nan],[4,2,"blue", 0,np.nan],
[5,1,"red", 88, np.nan],[5,1,"blue", "texas", np.nan],[5,2,"red",61, np.nan],[5, 2,"blue",5.5, np.nan]]

df = pd.DataFrame(data, columns = ['Test', "Question","Quality",'Answer', 'repeat_tag'])

使用以下代码旋转表格:

pivoted= pd.pivot_table(df,index=["Test","Question"],values =["Answer"], columns=["Quality"],aggfunc=np.sum)

当我旋转包含NaN值的表时,我的某些列仍然正确地用NaN填充,而另一些列则用零填充。因为我的数据包含一些实际上应该为零的值,所以我需要保留它们,并希望在旋转数据表之前将NaN值保留在数据表中存在的数据表中。

我从另一个问题(pandas pivot changes dtype)中读到:

“您的样本数据可能没有显示出来,但是您的数据透视操作的结果可能包含NaN,它们是浮点型的,因此该列的其余部分也被转换为由熊猫自动浮点,以便进行有效的计算。请注意, NaN由零(fill_value=0)填充,因此您看不到它们。”

我该如何解决?

0 个答案:

没有答案