Home > Back-end >  Is there a way to do this in Python?
Is there a way to do this in Python?

Time:08-03

I have a data frame that looks like this:

data = {'State': ['24', '24', '24', 
                  '24','24','24','24','24','24','24','24','24'],
        'County code': ['001', '001', '001', 
                        '001','002','002','002','002','003','003','003','003'],
        'TT code': ['123', '123', '123',
                    '123','124','124','124','124','125','125','125','125'],
        'BLK code': ['221', '221', '221',
                     '221','222','222','222','222','223','223','223','223'],
        'Age Code': ['1', '1', '2', '2','2','2','2','2','2','1','2','1']}
df = pd.DataFrame(data)

essentially I want to just have where only the TT code where the age code is 2 and there are no 1's. So I just want to have the data frame where:

'State': ['24', '24', '24', '24'],
'County code': ['002','002','002','002',],
'TT code': ['124','124','124','124',],
'BLK code': ['222','222','222','222'],
'Age Code': ['2','2','2','2']

is there a way to do this?

CodePudding user response:

This should work.

df111['Age Code'] = "2"

I am just wondering why the choice of string for valueType of integer

CodePudding user response:

IIUC, you want to keep only the TT groups where there are only Age groups with value '2'?

You can use a groupby.tranform('all') on the boolean Series:

df[df['Age Code'].eq('2').groupby(df['TT code']).transform('all')]

output:

  State County code TT code BLK code Age Code
4    24         002     124      222        2
5    24         002     124      222        2
6    24         002     124      222        2
7    24         002     124      222        2
  • Related