r_m
r_m

Reputation: 43

How to find the row with specified value in DataFrame

As I am newbie to deeper DataFrame operations, I would like to ask, how to find eg. the lowest campaign ID in this DataFrame per every customerid which is in this kind of DataFrame? As I learned, iteration should not be done in DataFrame.

    orderid  customerid  campaignid  orderdate              city state zipcode paymenttype  totalprice  numorderlines  numunits
0   1002854       45978        2141 2009-10-13            NEWTON    MA   02459          VI      190.00              3         3
1   1002855      125381        2173 2009-10-13      NEW ROCHELLE    NY   10804          VI       10.00              1         1
2   1002856      103122        2141 2011-06-02             MIAMI    FL   33137          AE       35.22              2         2
3   1002857      130980        2173 2009-10-14      E RUTHERFORD    NJ   07073          AE       10.00              1         1
4   1002886       48553        2141 2010-11-19         BALTIMORE    MD   21218          VI       10.00              1         1
5   1002887      106150        2173 2009-10-15          ROWAYTON    CT   06853          AE       10.00              1         1
6   1002888       27805        2173 2009-10-15      INDIANAPOLIS    IN   46240          VI       10.00              1         1
7   1002889       24546        2173 2009-10-15     PLEASANTVILLE    NY   10570          MC       10.00              1         1
8   1002890       43783        2173 2009-10-15  EAST STROUDSBURG    PA   18301          DB       29.68              2         2
9   1003004       15688        2173 2009-10-15   ROUND LAKE PARK    IL   60073          DB       19.68              1         1
10  1003044      130970        2141 2010-11-22        BLOOMFIELD    NJ   07003          AE       10.00              1         1
11  1003045       40048        2173 2010-11-22       SPRINGFIELD    IL   62704          MC       10.00              1         1
12  1003046       21927        2141 2010-11-22              WACO    TX   76710          MC       17.50              1         1
13  1003075      130971        2141 2010-11-22         FAIRFIELD    NJ   07004          MC       59.80              1         4
14  1003076        7117        2141 2010-11-22          BROOKLYN    NY   11228          AE       22.50              1         1

Upvotes: 2

Views: 73

Answers (1)

Josmoor98
Josmoor98

Reputation: 1811

Try the following

df.groupby('customerid')['campaignid'].min()

You can group unique values of customerid and subsequently find the minimum value per group for a given column using ['column_name'].min()

Upvotes: 2

Related Questions