简体   繁体   中英

How can I get a list of values for rows combined by groupby in a Pandas Dataframe?

Suppose I have the following dataframe:

#!/usr/bin/env python

import pandas as pd


df = pd.DataFrame([(1, 2, 1),
                   (1, 2, 2),
                   (1, 2, 3),
                   (4, 1, 612),
                   (4, 1, 612),
                   (4, 1, 1),
                   (3, 2, 1),
                   ],
                  columns=['groupid', 'a', 'b'],
                  index=['India', 'France', 'England', 'Germany', 'UK', 'USA',
                         'Indonesia'])
print(df)

which gives:

           groupid  a    b
India            1  2    1
France           1  2    2
England          1  2    3
Germany          4  1  612
UK               4  1  612
USA              4  1    1
Indonesia        3  2    1

Step 1

This step might not be necessary / be different than how I imagine it. I'm actually only interested in Step 2, but having this helps me to think about it and explain what I want.

I want to group the data by groupid ( df.groupby(df['groupid']) ) and get something like this:

    groupid  a    b
          1  [2]  [1, 2, 3]
          4  [1]  [612, 1]
          3  [2]  [1]

Step 2

Then I want to find all group IDs which have only one entry in column b and for which the entry is equal to 1 .

Similarly, I want to find all group IDs which have either multiple entries or one entry which is not 1 .

You can compare set s and then get values of index to list s:

mask = df.groupby('groupid')['b'].apply(set) == set([1])
print (mask)
groupid
1    False
3     True
4    False
Name: b, dtype: bool

i = mask.index[mask].tolist()
print (i)
[3]

j = mask.index[~mask].tolist()
print (j)
[1, 4]

For new column use map :

df['new'] = df['groupid'].map(df.groupby('groupid')['b'].apply(set) == set([1]))
print (df)

           groupid  a    b    new
India            1  2    1  False
France           1  2    2  False
England          1  2    3  False
Germany          4  1  612  False
UK               4  1  612  False
USA              4  1    1  False
Indonesia        3  2    1   True

old solution:

You can use transform with nunique for new Series with same size as original df, so is possible compare it with 1 for uniqueness and then chain another condition for compare with 1 :

mask = (df.groupby('groupid')['b'].transform('nunique') == 1) & (df['b'] == 1)
print (mask)
India        False
France       False
England      False
Germany      False
UK           False
USA          False
Indonesia     True
Name: b, dtype: bool

For unique values in list s:

i = df.loc[mask, 'groupid'].unique().tolist()
print (i)
[3]

j = df.loc[~mask, 'groupid'].unique().tolist()
print (j)
[1, 4]

Detail:

print (df.groupby('groupid')['b'].transform('nunique'))
India        3
France       3
England      3
Germany      2
UK           2
USA          2
Indonesia    1
Name: b, dtype: int64

IIUC you can apply list and check for length using .str ie

temp = df.groupby('groupid')['b'].apply(list).to_frame()

temp
                   b
groupid               
1            [1, 2, 3]
3                  [1]
4        [612, 612, 1]

mask = (temp['b'].str.len() == 1) & (temp['b'].str[0] == 1) 

temp[mask].index.tolist()
#[3]
temp[~mask].index.tolist()
#[1, 4]

I would go with

#group by the group id and than apply count for how many b entries are equal to 1 
groups = df.groupby("groupid").apply(lambda group:len([x for x in 
group["b"].values.tolist() if x == 1]))
#keep the groups containing 1 b equal to 1 
groups = groups[groups == 1]
#print the indecies of the result (the groupid values)
print groups.index.values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM