I'm pivoting a table with np.NaN values, those I would like to be preserved after the transformation, which is not happening at the moment.
Given the below table:
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", "large", "large", "small",
"small", "large", "small", "small",
"large"],
"D": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 7],
"E": [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]})
And now applying the pivot_table:
table = pd.pivot_table(df, values='D', index=['A', 'B'],
columns=['C'], aggfunc=np.sum, fill_value='HAHA').reset_index()
The output I get:
C A B large small
0 bar one 0 0.0
1 bar two 7 0.0
2 foo one 0 0.0
3 foo two HAHA 0.0
The output I was expecting:
C A B large small
0 bar one HAHA HAHA
1 bar two 7 HAHA
2 foo one HAHA HAHA
3 foo two HAHA HAHA
Question: Why is the method pivot_table populating only the group "foo - two - large" and the other groups are not having their np.NaN preserved, instead having it replaced by zero?