Reputation: 690
I have the following script:
import pandas as pd
ls = [
['A', 1, 'A1', 9],
['A', 1, 'A1', 6],
['A', 1, 'A1', 3],
['A', 2, 'A2', 7],
['A', 3, 'A3', 9],
['B', 1, 'B1', 7],
['B', 1, 'B1', 3],
['B', 2, 'B2', 7],
['B', 2, 'B2', 8],
['C', 1, 'C1', 9],
]
#convert to dataframe
df = pd.DataFrame(ls, columns = ["Main_Group", "Sub_Group", "Concat_GRP_Name", "X_1"])
#get count and sum of concatenated groups
df_sum = df.groupby('Concat_GRP_Name')['X_1'].agg(['sum','count']).reset_index()
#print in permutations formula to calculate different permutation combos
import itertools as it
perms = it.permutations(df_sum.Concat_GRP_Name)
def combute_combinations(df, colname):
l = []
import itertools as it
perms = it.permutations(df[colname])
for perm_pairs in perms:
#take in only the first three pairs of permuations and make sure
#the first column starts with A, secon with B, and third with C
if 'A' in perm_pairs[0] and 'B' in perm_pairs[1] and 'C' in perm_pairs[2]:
l.append([perm_pairs[0], perm_pairs[1], perm_pairs[2]])
return l
#apply function, this will generate a list of all of the permuation pairs
t = combute_combinations(df_sum, 'Concat_GRP_Name' )
#convert to dataframe and drop duplicate pairs
df2 = pd.DataFrame(t, columns = ["Item1", 'Item2', 'Item3']) .drop_duplicates()
I am not sure how to combine the components of a loop inside of an IF statement. From the example above, I knew that I had three different types of the Main_Group variable. Let's say that I didn't know how many unique values existed in the Main_Group column. How do I update the following IF statement to account for this?
if 'A' in perm_pairs[0] and 'B' in perm_pairs[1] and 'C' in perm_pairs[2]:
l.append([perm_pairs[0], perm_pairs[1], perm_pairs[2]])
I want each of the variable in it's own column. If I have 5 types of main group then I will have perm_pairs[0] to perm_pairs[4] in my IF statement. I was thinking about extracting the values in the Main_Group and turning this into a set. Then I would iterate through each value and use it's length to figure out the IF statement, but so far the logic is not working out. How do I iterate through the set and then update the IF statement?
Upvotes: 0
Views: 85
Reputation: 1864
To make the condition more dynamic, you can refactor your function like this:
import numpy as np
def combute_combinations(df, colname, main_group_series):
l = []
import itertools as it
perms = it.permutations(df[colname])
# Provides sorted list of unique values in the Series
unique_groups = np.unique(main_group_series)
for perm_pairs in perms:
#take in only the first three pairs of permuations and make sure
#the first column starts with A, secon with B, and third with C
if all([main_group in perm_pairs[ind] for ind, main_group in enumerate(unique_groups)]):
l.append([perm_pairs[ind] for ind in range(unique_groups.shape[0])])
return l
Then you can just call the function as before, but include the series of the main group column
t = combute_combinations(df_sum, 'Concat_GRP_Name', df['Main_Group'])
Upvotes: 1