1
到数据帧的子集新行我有以下数据框:添加使用熊猫
Customer ProductID Count
John 1 25
John 6 50
Mary 2 15
Mary 3 35
我想我的输出看起来像这样:
Customer ProductID Count
John 1 25
John 2 0
John 3 0
John 6 50
Mary 1 0
Mary 2 15
Mary 3 35
Mary 6 0
我所试图做的是确定唯一ProductID
从数据帧开始
unique_ID = pd.unique(df.ProductID.ravel())
print (unique_ID) = array([1,6,2,3])
由于ProductID
2,3不存在客户约翰,我将客户姓名分成数据帧
df1 = df[df['Customer']=='John']
df2 = df[df['Customer']=='Mary']
打印DF1
Customer ProductID Count
John 1 25
John 6 50
打印DF2
Customer ProductID Count
Mary 2 15
Mary 3 35
我想补充ProductID
2,3约翰和ProductID
1 ,6给玛丽,并且将Count
设置为0,这些是ProductID
,如上面我所期望的输出所示。