Issue
I am having a hard time with being able to change all the values in one column where another column has a unique ID associated with the values that need to be changed. For example...
col1 | col2
a x
a x
a y
a y
b 'none'
b x
b x
b z
b z
I need to be able to check where col2 contains 'none' and then change all of the values in col2 to 'none' where col1 is equal to 'b'. Please bear in mind that the values I provided here in the example are not the real values, they are much longer and there are 100s of 1000s of rows, so checked the names manually is not an option. This would be the desired outcome...
col1 | col2
a x
a x
a y
a y
b 'none'
b 'none'
b 'none'
b 'none'
b 'none'
I am not sure how to even start with this conditional statement in pandas. Your help will be greatly appreciated. I am slowly building my knowledge of Pandas methods.
Solution
Use .loc
twice:
df.loc[df['col1'].isin(df.loc[df['col2'].eq('none'), 'col1']), 'col2'] = 'none'
print(df)
# Output:
col1 col2
0 a x
1 a x
2 a y
3 a y
4 b none
5 b none
6 b none
7 b none
8 b none
Step-by-step:
# Extract rows where col2 is 'none'
>>> df.loc[df['col2'].eq('none'), 'col1']
4 b
Name: col1, dtype: object
# Create a boolean mask
>>> df['col1'].isin(df.loc[df['col2'].eq('none'), 'col1'])
0 False
1 False
2 False
3 False
4 True
5 True
6 True
7 True
8 True
Name: col1, dtype: bool
# Set 'none' to all rows from same "group"
>>> df.loc[df['col1'].isin(df.loc[df['col2'].eq('none'), 'col1']), 'col2'] = 'none'
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.