Looking to preserve NaN values when changing the shape of the dataframe.
These two questions may be related:
- How to preserve NaN instead of filling with zeros in pivot table?
- How to make two NaN as NaN after the operation instead of making it zero?
but not been able to use the answers provided - can I set a min count for np.sum somehow?
import pandas as pd
import numpy as np
df = pd.DataFrame([['Y1', np.nan], ['Y2', np.nan], ['Y1', 6], ['Y2',8]], columns=['A', 'B'], index=['1988-01-01','1988-01-01', '1988-01-04', '1988-01-04'])
df.index.name = 'Date'
dfpivot_df = pd.pivot_table(df, values='B', index=['Date'], columns=['A'],aggfunc=np.sum)
pivot_df
The output is:
A Y1 Y2
Date
1988-01-01 0.0 0.0
1988-01-04 6.0 8.0
and the desired output is:
A Y1 Y2
Date
1988-01-01 NaN NaN
1988-01-04 6.0 8.0