python - Subset dataframe based on field -


mukey   cokey     hzdept_r  hzdepb_r 422927  11090397    0        20 422927  11090397    20       71 422927  11090397    71       152 422927  11090398    0        18 422927  11090398    18       117 422927  11090398    117      152 

i subset dataframe above, such first set of cokey's (in case 11090397) selected. of course, since sample dataset, solution needs scale larger versions of such dataframe.

in case, resulting dataset should be:

mukey   cokey     hzdept_r  hzdepb_r 422927  11090397    0        20 422927  11090397    20       71 422927  11090397    71       152 

i have tried using groupby, not sure how select first cokey value there.

another method take first unique value:

in [97]:  df[df['cokey'] == df['cokey'].unique()[0]] out[97]:     mukey     cokey  hzdept_r  hzdepb_r 0  422927  11090397         0        20 1  422927  11090397        20        71 2  422927  11090397        71       152 

you use integer based indexing first value filtering:

in [99]:  df[df['cokey'] == df['cokey'].iloc[0]] out[99]:     mukey     cokey  hzdept_r  hzdepb_r 0  422927  11090397         0        20 1  422927  11090397        20        71 2  422927  11090397        71       152 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -