I have a pandas dataframe like this:
df = [[1, 'a1 - d1'],
[1, 'b1 - c1'],
[1, 'd1 - c1'],
[1, 'a1 - b1'],
[1, 'b1 - d1'],
[1, 'c1 - a1'],
[2, 'y1 - z1'],
[2, 'x1 - z1'],
[2, 'z1 - x1']]
# Create the pandas DataFrame
df = pd.DataFrame(df, columns=['group_id', 'combinations'])
df
group_id | Combinations |
---|---|
1 | a1 - d1 |
1 | b1 - c1 |
1 | d1 - c1 |
1 | a1 - b1 |
1 | b1 - d1 |
1 | c1 - a1 |
2 | y1 - z1 |
2 | x1 - y1 |
2 | z1 - x1 |
- Everyday I want to use 1 combination of each group (represented by group_id) for n = 30 days. I want the code to have variable n = 30, so that I can vary this number later if required.
- Some groups might have more than n = 30 combinations and some groups may have less than n = 30 combinations to begin with.
Expected output:
days | group_id | user_id |
---|---|---|
day 1 | 1 | a1 - d1 |
day 2 | 1 | b1 - c1 |
day 3 | 1 | d1 - c1 |
day 4 | 1 | a1 - b1 |
day 5 | 1 | b1 - d1 |
day 6 | 1 | c1 - a1 |
day 7 | 1 | y1 - z1 |
.. | .. | .. |
day 19 | 1 | a1 - d1 |
day 20 | 1 | b1 - c1 |
day 21 | 1 | d1 - c1 |
day 22 | 1 | a1 - b1 |
day 23 | 1 | b1 - d1 |
day 24 | 1 | c1 - a1 |
day 25 | 1 | y1 - z1 |
day 26 | 1 | x1 - y1 |
day 27 | 1 | z1 - x1 |
day 28 | 1 | a1 - d1 |
day 29 | 1 | b1 - c1 |
day 30 | 1 | d1 - c1 |
day 1 | 2 | .. |
A simple
pd.concat([x]*2)
repeats the whole dataframe twice of n times, which is not the output that I am expecting. Is there any way to repeat such a pattern??
Update code dump that i have tried so far.
Basically querying each a every group and repeating sequence of that and you know somehow merging them all in the end. Probably the most inefficient solution. But am not sure how else to do this...
group_list = set(df['group_id'])
for i in group_list:
df1 = df.query(f'group_id == {i}')
# assuming the worst case scenario to be a group containing only 1 combination
df1 = pd.concat([df1]*30)
df1 = df1.head(30)
for
loop redefines df1 each time.