Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: Wrong number of items passed 5, placement implies 1

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()))
like image 672
Mathanraj-Sharma Avatar asked Nov 24 '25 19:11

Mathanraj-Sharma


1 Answers

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!

like image 142
Arkady. A Avatar answered Nov 27 '25 08:11

Arkady. A