问题
I have the following dataset:
ID Asset Boolean
1 "A" True
1 "B" False
1 "B" False
2 "A" True
3 "A" True
3 "A" True
3 "B" False
3 "B" False
4 "A" True
4 "A" True
5 "A" True
5 "B" False
I want to add another column, which should evaluate to True only if all values in the column Boolean
evaluate to True for the same ID
.
So something like this:
ID Asset Boolean Check
1 "A" True False
1 "B" False False
1 "B" False False
2 "A" True True
3 "A" True False
3 "A" True False
3 "B" False False
3 "B" False False
4 "A" True True
4 "A" True True
5 "A" True False
5 "B" False False
I want to keep the original dataset for filter options.
I could not figure out, how to iterate through the Boolean
column taking the ID column into consideration.
回答1:
You can GroupBy and transform with all:
df['Check'] = df.groupby('ID').Boolean.transform('all')
print(df)
ID Asset Boolean Check
0 1 A True False
1 1 B False False
2 1 B False False
3 2 A True True
4 3 A True False
5 3 A True False
6 3 B False False
7 3 B False False
8 4 A True True
9 4 A True True
10 5 A True False
11 5 B False False
来源:https://stackoverflow.com/questions/60394026/adding-new-column-to-pandas-df-based-on-condition