I'm trying to count the number of times a combination of strings appear in each row of a dataframe. Each ID uses a number of methods (some IDs use more methods than others) and I want to count the number of times any two methods have been combined together.
# df is from csv and has blank cells - I've used empty strings to demo here
df = pd.DataFrame({'id': ['101', '102', '103', '104'],
'method_1': ['HR', 'q-SUS', 'PEP', 'ET'],
'method_2': ['q-SUS', 'q-IEQ', 'AUC', 'EEG'],
'method_3': ['SC', '', 'HR', 'SC'],
'method_4': ['q-IEQ', '', 'ST', 'HR'],
'method_5': ['PEP', '', 'SC', '']})
print(df)
id method_1 method_2 method_3 method_4 method_5
0 101 HR q-SUS SC q-IEQ PEP
1 102 q-SUS q-IEQ
2 103 PEP AUC HR ST SC
3 104 ET EEG SC HR
I want to end up with a table that looks something like this:
| Method A | Method B | Number of Times Combined |
|---|---|---|
| HR | SC | 3 |
| HR | q-SUS | 1 |
| HR | PEP | 2 |
| q-IEQ | q-SUS | 2 |
| EEG | ET | 1 |
| EEG | SC | 1 |
| etc. | etc. | etc. |
So far I've been trying variations of this code using itertools.combinations and collections Counter:
import numpy as np
import pandas as pd
import itertools
from collections import Counter
def get_all_combinations_without_nan(row):
# remove nan - this is for the blank csv cells
set_without_nan = {value for value in row if isinstance(value, str)}
# generate all combinations of values in row
all_combinations = []
for index, row in df.iterrows():
result = list(itertools.combinations(set_without_nan, 2))
all_combinations.extend(result)
return all_combinations
# get all possible combinations of values in a row
all_rows = df.apply(get_all_combinations_without_nan, 1).values
all_rows_flatten = list(itertools.chain.from_iterable(all_rows))
count_combinations = Counter(all_rows_flatten)
print(count_combinations)
It's doing something, but it seems to be counting multiple times or something (it's counting more combinations than are actually there. I've had a good look on Stack, but can't seem to solve this - everything seems really close though!
I hope someone can help - Thanks!