Python Pandas groupby assignment of object dtypes -


this python dataframe:

df = pd.dataframe({'id': [3553102778, 3553102958, 3553103948, 3553103948, 3553104038, 3553104038, 3553104128, 3553104218, 3557580098], 'based on': ['ctr', 'ctr', 'conv rate', 'ctr', 'conv rate', 'ctr', 'ctr', 'ctr', 'ctr']}, columns=['id', 'based on']) 

produces dataframe looks this:

           id   based on 0  3553102778        ctr 1  3553102958        ctr 2  3553103948  conv rate 3  3553103948        ctr 4  3553104038  conv rate 5  3553104038        ctr 6  3553104128        ctr 7  3553104218        ctr 8  3557580098        ctr 

notice how id 3553103948 , 3553104038 have both conv rate , ctr?

how group id assign based on value of conv rate, ctr this?

           id        based on 0  3553102778             ctr 1  3553102958             ctr 2  3553103948  conv rate, ctr 4  3553104038  conv rate, ctr 6  3553104128             ctr 7  3553104218             ctr 8  3557580098             ctr 

i think it's kind of groupby , assignment one-liner don't know how detect if other ids exist outside row equal current row's id.

maybe groupby in conjunction apply? i'm not familiar apply yet.

you can use apply on groups, allows transform group. means function inside returns each set of entries has same id.

group apply

g = df.groupby("id") s = g["based on"].apply(lambda group: group.values) s.to_frame()

s series, , may want convert dataframe


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -