Convert first False value to True using categorical group with 70 unique values

Question:

I am trying to convert the first occurrence of False to True in a column in a Pandas Dataframe. The Column in question contains True, False and null values. My current code is:

df.loc[df.groupby('categorical_col')[''].idxmin(), 'target_col'] = True

However this gives me the following error:

TypeError: reduction operation 'argmin' not allowed for this dtype

How can I convert the first occurrence of False to True while incorporating the categorical group?

Edit sample data:

categorical_col target_col
A TRUE
A TRUE
A TRUE
A TRUE
A FALSE
B TRUE
B
B
B TRUE
B FALSE
Asked By: peho15ae

||

Answers:

Problem is column target_col is not boolean, but filled by strings:

print (df)
   categorical_col  target_col
0                1       False
1                1       False
2                2        True

print (df.target_col.dtypes)
object

For boolean compare by string 'True':

df['target_col'] = df['target_col'].eq('True')

df.loc[df.groupby('categorical_col')['target_col'].idxmin(), 'target_col'] = True
print (df)
   categorical_col  target_col
0                1        True
1                1       False
2                2        True
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.