Click to view data sampleI am trying to replace the Item_Visibility values with the Item_Visibility of each Item_Identifier (each item type). But it throws an error:
ValueError: Wrong number of items passed 5, placement implies 1
data['Item_Visibilty'] = data.groupby('Item_Identifier').transform(
lambda x: x.replace(x.mean()))
As far as I understood your question: you want to put the mean of Item_Visibility for each unique value of Item_Identifier into Item_Visibility column. I guess, Item_Identifier, in this case means a group of items because in other way it doesn't make sense to group them and then take the mean.
Answering you question:
# sample data creation
data = pd.DataFrame(np.random.rand(4000,2),columns=['Item_Identifier','Item_Visibility'])
data.loc[:,'Item_Identifier']= data.loc[:,'Item_Identifier'].apply(
lambda x: 'id1' if x> 0.4 else 'id2')
# creating map_table so we could map values
map_table = data.groupby('Item_Identifier').mean()
# mapping values
data.loc[:,'Item_Visibility'] = data.loc[:,'Item_Identifier'].map(
map_table.to_dict()['Item_Visibility'])
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With