Search code examples
pythonpandasdataframegroup-bypandas-groupby

I want to check which Vin has abs value in column Module


enter image description here

I want to check which Vin has abs value in column Module. If it has then in the Result column I want Yes wherever that number is present in the column Vin.


Solution

  • You can first check all the rows where Module == "abs", and then group the result by Vin, and use transform('any') to convert each group to rows of True if the group contains a single True, rows of False otherwise:

    df['Result'] = df['Module'].eq('abs').groupby(df['Vin']).transform('any').map({True: 'Yes', False: 'No'})