Filter rows after groupby pandas

前端 未结 4 1998
耶瑟儿~
耶瑟儿~ 2021-01-05 09:58

I have a table in pandas:

import pandas as pd

df = pd.DataFrame({
    \'LeafID\':[1,1,2,1,3,3,1,6,3,5,1],
    \'pidx\':[10,10,300,10,30,40,20,10,30,45,20],         


        
4条回答
  •  旧时难觅i
    2021-01-05 10:28

    First of all, your output shows you don't want to do a groupby. Read up on what groupby does. What you need is:

    df2 = df[df['pidx']<=20]
    df2.sort_index(by = 'pidx')
    

    This will give you your exact result. Read up on pandas indexing and functions. In fact go and read the whole introduction on pandas. It will not take much time.

    Row operations are also simple using indexing:

    df2['final_score']= 0.4*df2['count'] + 0.6*df2['score']
    

提交回复
热议问题