I would like to create a new column and assign value for each group (in this case is Color), and in each group:
if Function column is View and Access column is no, the value for the new column will be 'No' for this group;
if Function column = View and Access column = yes, the value for new column will be same as 'Access' for the rows of the group
Data:
Type | Color | Function | Access |
---|---|---|---|
A | Blue | Add | yes |
A | Blue | View | no |
A | Red | Add | no |
A | Red | View | yes |
B | Blue | Add | yes |
B | Blue | View | no |
Desired Outcome:
Type | Color | Function | Access | New Column |
---|---|---|---|---|
A | Blue | Add | yes | no |
A | Blue | View | no | no |
A | Red | Add | no | no |
A | Red | View | yes | yes |
B | Blue | Add | yes | no |
B | Blue | View | no | no |
# I created a new column first before grouping them
data['New Column'] = ''
data_grouped = data.groupby(['Type', 'Color']
# attempted to loop but stuck here #
for group_name, df_group in data_grouped:
print(format(group_name))
for row_index, row in df_group.iterrows():
if (row['Function'] == 'View') & (row['Access'] == 'no'):
row['New Column'] = 'no'
print(col, column_type)