Home > Mobile >  There should be one and only one value in column B for every value in column A - Pandas
There should be one and only one value in column B for every value in column A - Pandas

Time:09-22

I have a data frame as shown in image:

enter image description here

I want an output similar to the "Value" Column. What it means is For every value in column A, there can only be one and only one value in column B. Even if value in column A repeats, the value in column B should also repeat. Please help

CodePudding user response:

Use transform to set the first 'B' value encountered for each 'A' value to each row then test if this first value is equal to 'B' value.

df['V'] = df['B'] == df.groupby('A')['B'].transform('first')

Output:

>>> df
   A  B      V
0  1  a   True
1  1  a   True
2  2  a   True
3  3  b   True
4  1  a   True
5  2  b  False
6  3  c  False
7  1  d  False

Details:

>>> df.groupby('A')['B'].transform('first')
0    a
1    a
2    a
3    b
4    a
5    a
6    b
7    a
Name: B, dtype: object
  • Related