Skip to content Skip to sidebar Skip to footer

How To Do Intersection Of Dataframes In Pandas

I have a dataframe like following : The columns are : Title ASIN State SellerSKU Quantity FBAStock QuantityToShip I have another dataframe which contains a subse

Solution 1:

I believe want transform for new column by size per groups with multiple column Quantity by *=:

originalDF = pd.DataFrame({'State':list('aaabbb'),
                           'ASIN':list('cfcccc'),
                           'Quantity':[100] * 6})


originalDF['Quantity'] *= (originalDF.groupby(['State' ,'ASIN' , 'Quantity'])['State']
                                    .transform('size'))

print (originalDF)
  State ASIN  Quantity
0     a    c       200
1     a    f       100
2     a    c       200
3     b    c       300
4     b    c       300
5     b    c       300

Detail:

print ((originalDF.groupby(['State' ,'ASIN' , 'Quantity'])['State']
                                    .transform('size')))

0    2
1    1
2    2
3    3
4    3
5    3
Name: State, dtype: int64

Post a Comment for "How To Do Intersection Of Dataframes In Pandas"