我有一个具有不同产品ID,对应prod_descriptions和数量的大数据框。有些产品ID包含子产品(prod_desc2,prod_desc3 ...等)没有产品ID或未映射到主产品ID(1111) ,333),其值在prod_id列中为空,如示例DF中所示。
Sample DF
prod_id prod_description col1 col2 col3 col4 col5
1111 prod_desc1 10 20 30 45 25
prod_desc2 15 17 16 28 nan
prod_desc3 15 17 5 nan nan
2222 prod_desc1 5 10 15 7 10
2223 prod_desc1 15 10 25 10 10
333 prod_desc1 10 15 20 23 25
prod_desc2 25 5 25 10 nan
我想将prod_desc2和prod_desc3的数量汇总到prod_desc1级别,并在所需的输出中显示一个新的DF以及其他prod _id(2222,2223),以便每个产品ID都有一行的累加和其子产品。
Desired Output
prod_id prod_description col1 col2 col3 col4 col5
1111 prod_desc1 40 54 51 73 25
2222 prod_desc1 5 10 15 7 10
2223 prod_desc1 15 10 25 10 10
333 prod_desc1 35 20 45 33 25
下面是我尝试过的“部分”代码,在累加prod id row和no_prod_id列的列值并将它们与其他prod_ids一起保存到新的数据框中时遇到了麻烦。
Empty rows were filled with no_prod_id
prod_id prod_description col1 col2 col3 col4 col5
1111 prod_desc1 10 20 30 45 25
no_prod_id prod_desc2 15 17 16 28 nan
no_prod_id prod_desc3 15 17 5 nan nan
2222 prod_desc1 5 10 15 7 10
2223 prod_desc1 15 10 25 10 10
333 prod_desc1 10 15 20 23 25
no_prod_id prod_desc2 25 5 25 10 nan
null_value_count=[]
rolled_up_values=[]
for i in df.index:
if df.iloc[i,0]=="no_prod_id": #pick no_prod_id row
x=df.iloc[i,:] #save null value row
if x.isnull().sum().sum()==df.shape[1]: # check if no_prod_id is having all nulls
null_value_cunt.append(i) #save index for later deleting it from DF
else:
if df.iloc[i-1,0]!= "no_prod_id": #check previus row has main prod id
y=df.iloc[i-1,:] # save main prod id row
for val in range(1,len(y)): #get each value of main prod id
rolled_up_values.append(x[val]+y[val]) #sum with no_prod_id value save the out in
#list for updating in a new DF
答案 0 :(得分:1)
第一个ffill
df['prod_id'] = df['prod_id'].ffill()
print(df)
prod_id prod_description col1 col2 col3 col4 col5
0 1111.0 prod_desc1 10 20 30 45.0 25.0
1 1111.0 prod_desc2 15 17 16 28.0 NaN
2 1111.0 prod_desc3 15 17 5 NaN NaN
3 2222.0 prod_desc1 5 10 15 7.0 10.0
4 2223.0 prod_desc1 15 10 25 10.0 10.0
5 333.0 prod_desc1 10 15 20 23.0 25.0
6 333.0 prod_desc2 25 5 25 10.0 NaN
然后我们删除您的prod_description并按剩余的列进行分组
df_new = df.drop('prod_description',axis=1).groupby('prod_id').sum().reset_index()
df_new.insert(1,'prod_description','prod_desc1') # reinsert columns.
结果,请注意,我刚刚添加了自定义排序以匹配您的输出。
idx = df_new['prod_id'].astype(str).str[1].astype(int).sort_values().index
print(df_new.loc[idx])
prod_id prod_description col1 col2 col3 col4 col5
1 1111.0 prod_desc1 40 54 51 73.0 25.0
2 2222.0 prod_desc1 5 10 15 7.0 10.0
3 2223.0 prod_desc1 15 10 25 10.0 10.0
0 333.0 prod_desc1 35 20 45 33.0 25.0
或正如anky_91所指出的那样,我们可以使用.assign
和sort=False
df['prod_id'] = df['prod_id'].ffill()
df.groupby("prod_id", sort=False, as_index=False).sum().assign(
prod_description="prod_desc1"
).reindex(df.columns, axis=1)
结果
prod_id prod_description col1 col2 col3 col4 col5
0 1111.0 prod_desc1 40 54 51 73.0 25.0
1 2222.0 prod_desc1 5 10 15 7.0 10.0
2 2223.0 prod_desc1 15 10 25 10.0 10.0
3 333.0 prod_desc1 35 20 45 33.0 25.0