I have a Pandas df with 2 columns:
name Count_Relationship
0 allicin DOWNREGULATE: 1
1 allicin DOWNREGULATE: 2
2 allicin UPREGULATE: 1 | DOWNREGULATE: 1
3 aspirin UPREGULATE: 5 | DOWNREGULATE: 1
4 albuterol DOWNREGULATE: 1
5 albuterol UPREGULATE: 3
I would like to filter out only the rows where if I group by the 'names' and count in the 'Count_Relationship' column the amount of DOWNREGULATE to be more than the amount of the UPREGULATE. In this case allicin would have DOWREGULATE 1+2+1=4 and UPREGULATE =1 so num_downregulate>num_upregulate, while in the rest(aspirin,albuterol) that's not the case. I would like to return this filtered df:
name Count_Relationship
0 allicin DOWNREGULATE: 1
1 allicin DOWNREGULATE: 2
2 allicin UPREGULATE: 1 | DOWNREGULATE: 1
The column Count_Relationship is a string, so I would have to parse the number part of the string and convert it to int.
I tried this:
import pandas as pd
data = {'name': ['allicin', 'allicin', 'allicin', 'aspirin', 'albuterol', 'albuterol'],
'Count_Relationship': ['DOWNREGULATE: 1', 'DOWNREGULATE: 2', 'UPREGULATE: 1 | DOWNREGULATE: 1', 'UPREGULATE: 5 | DOWNREGULATE: 1', 'DOWNREGULATE: 1' , 'UPREGULATE: 3']
}
df = pd.DataFrame(data)
substances = df["name"].tolist()
substances = list(set(substances)) # to get the unique names
result_substances = []
for substance in (substances):
try:
numberOfdownregulate = df[(df["name"] == substance) & (\
(df["Count_Relationship"].str.match(pat = '("DOWNREGULATE:"([0-9]))')).values[0].astype(int)
except:
pass
try:
numberOfupregulate = df[(df["name"] == substance) & (\
(df["Count_Relationship"].str.match(pat = '("UPREGULATE:"([0-9]))')).values[0].astype(int)
except:
pass
result = numberOfdownregulate - numberOfupregulate
if result > 0:
result_substances.append(substance)
df_filtered = df[df["name"].isin(result_substances)]
but I get a syntax error at the line numberOfdownregulate where my regex is. How can fix the algorithm? thanks so much