Reputation: 23
Hi :) I would like to know if for each modality (df.A == 'First' and df.B = 'Second'), if for the column df.B the rows are equal (or not) to the first row (df['B'][0]).
A | B | C |
---|---|---|
First | 1 | None |
First | 2 | None |
First | 3 | None |
Second | 2 | None |
Second | 2 | None |
Second | 2 | None |
The output should be :
A | B | C |
---|---|---|
First | 1 | None |
First | 2 | False |
First | 3 | False |
Second | 2 | None |
Second | 2 | True |
Second | 2 | True |
I've tried this code :
def equal(df) :
arr = df['A'].unique()
arr = arr.tolist()
dfd = df.copy()
for i in arr :
for m in range(1, len(dfd['A']==i)) :
if dfd['B'][m] == dfd['B'][0] :
dfd['C'][m] == True
else :
dfd['C'][m] == False
equal(HoldingsLegal)
But I got this error :
:9: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
Thank you for your help.
Upvotes: 1
Views: 48
Reputation: 4761
IIUC this is what you want:
df['C'] = df.groupby('A')['B'].transform("nunique").eq(1)
# A B C
#0 First 1 False
#1 First 2 False
#2 First 3 False
#3 Second 2 True
#4 Second 2 True
#5 Second 2 True
If you need to turn the first values of every group to None
, you can do this:
df.loc[df.groupby('A').head(1).index,'C'] = None
A B C
#0 First 1 NaN
#1 First 2 0.0
#2 First 3 0.0
#3 Second 2 NaN
#4 Second 2 1.0
#5 Second 2 1.0
Upvotes: 2