python - Group by one column and find first of two columns pandas -


i have 1 dataframe geomerge, need group 1 column grpno. , select first of column maxofcount percent , first of column state code , display grpno. also. have rename them firstofmaxofstate count percent , firstofstate code

my input dataframe:

      count percent  grpno. state code  maxofcount percent 0          14.78       1         ca               14.78 1           0.00       2         ca                0.00 2           0.00       2         fl                0.00 3           8.80       3         ca                8.80 4           0.00       6         nc                0.00 5           0.00       5         nc                0.00 6          59.00       4         ma               59.00 

my output dataframe:

      firstofmaxofstate count percent  state pool number firstofstate code 0                            14.78                  1                ca 1                             0.00                  2                ca 2                             8.80                  3                ca 3                            59.00                  4                ma 4                             0.00                  5                nc 5                             0.00                  6                nc 

can on this?

you can use groupby.first:

import pandas pd  df = pd.dataframe({     'a': [1, 1, 2, 2],     'b': [0, 1, 2, 3],     'c': [2, 3, 1, 4]})  >>> df.groupby(df.a).agg({'b': 'first', 'c': 'first'})     c   b        1   2   0 2   1   2 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -