简体   繁体   中英

Setting Group Value in Pandas Groupby

Given an example dataframe df, when grouping by 'ind' I want to set the values in a group to 'Hat' if 'Hat' is in the group. ie

ind | val              ind | val
----------             ---------
1   | Hat               1  | Hat
1   | Cat               1  | Hat
1   | Cat        - >    1  | Hat
2   | Dog               2  | Dog
2   | Log               2  | Log
3   | Hat               3  | Hat
3   | Hat               3  | Hat

I have the following code to tell me if 'Hat' is in a group, but whats the best way to set every value in that group to hat?

for i, x in df.groupby('ind'):
     if(x['val'].str.contains('Hat').any()):   

One way without groupby or lambda is to use pd.DataFrame.loc :

import pandas as pd

df = pd.DataFrame({'ind': [1, 1, 1, 2, 2, 3, 3],
                   'val': ['Hat', 'Cat', 'Cat', 'Dog', 'Log', 'Hat', 'Hat']})

idx = set(df.loc[df['val'].str.contains('Hat', regex=False), 'ind'])

df.loc[df['ind'].isin(idx), 'val'] = 'Hat'

   ind  val
0    1  Hat
1    1  Hat
2    1  Hat
3    2  Dog
4    2  Log
5    3  Hat
6    3  Hat
In [101]: (df.groupby('ind')['val']
             .transform(lambda x: ['Hat'] * len(x) if x.str.contains('Hat').any() else x))
Out[101]:
0    Hat
1    Hat
2    Hat
3    Dog
4    Log
5    Hat
6    Hat
Name: val, dtype: object

What I will do

df.loc[df.val.eq('Hat').groupby(df.ind).transform('any'),'val']='Hat'
df
   ind  val
0    1  Hat
1    1  Hat
2    1  Hat
3    2  Dog
4    2  Log
5    3  Hat
6    3  Hat 

只是

df.groupby("ind").transform(lambda k: "hat" if "hat" in k.values else k.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