python - how to subset pandas dataframe on a condition -
i have pandas dataframe this:
buyer_id meal_type 139 veg 140 nonveg 140 veg 36 nonveg 79 veg 79 nonveg 79 nonveg 72 nonveg 72 nonveg 65 nonveg 65 veg
now want buyer_id
has veg
meal type,all buyer_id
has veg , nonveg
meal type , buyer_id
has nonveg
meal type.
so,
139 veg 140 veg , nonveg 36 nonveg 79 veg , nonveg
and on. doing following in python buyer ids have veg
, nonveg
or both.
segments_data.buyer_id[segments_data['meal_type']=='veg' & segments_data['meal_type']=='nonveg']
but doesn't work. please help.
grouping buyer_id
, converting meal_type
set works:
>>> df.groupby('buyer_id')['meal_type'].apply(set).str.join(' , ') buyer_id 36 nonveg 65 veg , nonveg 72 nonveg 79 veg , nonveg 139 veg 140 veg , nonveg name: meal_type, dtype: object
Comments
Post a Comment