I have following dataframe
A | B | C | D | E |
---|---|---|---|---|
-0.1 | 0 | 0.2 | 0 | 4 |
0 | 0 | -1 | -2 | 5 |
I would like an output as following based on A,B belong to category X and C,D,E belong to category Y -
A | B | C | D | E |
---|---|---|---|---|
-cat X | 0 | +cat Y | 0 | +cat Y |
0 | 0 | -cat Y | -cat Y | +cat Y |
It basically checks the column name and assigns a category and checks the value to assign a sign. Is there any easy way to do this in Python? I am using COLAB, so probably have latest version.
One way could be to just choose whatever columns you want and then perform check like below:
import numpy as np
for col in ['A','B']:
df[col] = [x if x == 0 else '-Cat X' if x < 0 else '+ Cat X' for x in df[col]]
for col in ['C','D','E']:
df[col] = [x if x == 0 else '-Cat Y' if x < 0 else '+ Cat Y' for x in df[col]]
This should produce the required result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.