unique combinations of values in selected columns in pandas data frame and count

后端 未结 4 633
无人及你
无人及你 2020-11-27 10:19

I have my data in pandas data frame as follows:

df1 = pd.DataFrame({\'A\':[\'yes\',\'yes\',\'yes\',\'yes\',\'no\',\'no\',\'yes\',\'yes\',\'yes\',\'no\'],
            


        
4条回答
  •  情歌与酒
    2020-11-27 11:07

    I haven't done time test with this but it was fun to try. Basically convert two columns to one column of tuples. Now convert that to a dataframe, do 'value_counts()' which finds the unique elements and counts them. Fiddle with zip again and put the columns in order you want. You can probably make the steps more elegant but working with tuples seems more natural to me for this problem

    b = pd.DataFrame({'A':['yes','yes','yes','yes','no','no','yes','yes','yes','no'],'B':['yes','no','no','no','yes','yes','no','yes','yes','no']})
    
    b['count'] = pd.Series(zip(*[b.A,b.B]))
    df = pd.DataFrame(b['count'].value_counts().reset_index())
    df['A'], df['B'] = zip(*df['index'])
    df = df.drop(columns='index')[['A','B','count']]
    

提交回复
热议问题