1

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)
5
  • 1
    Could you drop in what code you have so far?
    – Carbon
    Jun 4 at 15:55
  • updated @Carbon Jun 4 at 16:08
  • I'm only seeing group 2 in df1 at the end of the loop- is this intended? The for loop redefines df1 each time.
    – Carbon
    Jun 4 at 16:22
  • Redefining is exactly what I am looking for. For each group i want store 30 days worth of combinations. The sequence has to repeat if number of rows for that group < 30. Jun 4 at 16:30
  • Ah, ok, then this is easy. One sec
    – Carbon
    Jun 4 at 16:32

1 Answer 1

1

Here, this is not so bad. You're looking to create a sequence that repeats of the length of the number of rows in each group. We'll first split our df into groups, then iterate through each group. We'll use some tricks from itertools (list(islice(cycle(range(4)),7)) -> [0, 1, 2, 3, 0, 1, 2]) to get our sequence to repeat and get indices of the rows we want. We'll then select those rows and set the index to the day of the month.

import pandas as pd
from itertools import cycle,islice

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'])
grouped = df.groupby('group_id')

out = {}

for grp_id, grp_data in grouped:  # 1,2
    indexes = islice(cycle(range(len(grp_data))), 30)
    result = pd.DataFrame(grp_data.iloc[indexes])
    result.index = range(1,31)
    out[grp_id] = result

print(out)
{1:     group_id combinations
1          1      a1 - d1
2          1      b1 - c1
3          1      d1 - c1
4          1      a1 - b1
5          1      b1 - d1
6          1      c1 - a1
7          1      a1 - d1
8          1      b1 - c1
9          1      d1 - c1
10         1      a1 - b1
11         1      b1 - d1
12         1      c1 - a1
13         1      a1 - d1
14         1      b1 - c1
15         1      d1 - c1
16         1      a1 - b1
17         1      b1 - d1
18         1      c1 - a1
19         1      a1 - d1
20         1      b1 - c1
21         1      d1 - c1
22         1      a1 - b1
23         1      b1 - d1
24         1      c1 - a1
25         1      a1 - d1
26         1      b1 - c1
27         1      d1 - c1
28         1      a1 - b1
29         1      b1 - d1
30         1      c1 - a1, 2:     group_id combinations
1          2      y1 - z1
2          2      x1 - z1
3          2      z1 - x1
4          2      y1 - z1
5          2      x1 - z1
6          2      z1 - x1
7          2      y1 - z1
8          2      x1 - z1
9          2      z1 - x1
10         2      y1 - z1
11         2      x1 - z1
12         2      z1 - x1
13         2      y1 - z1
14         2      x1 - z1
15         2      z1 - x1
16         2      y1 - z1
17         2      x1 - z1
18         2      z1 - x1
19         2      y1 - z1
20         2      x1 - z1
21         2      z1 - x1
22         2      y1 - z1
23         2      x1 - z1
24         2      z1 - x1
25         2      y1 - z1
26         2      x1 - z1
27         2      z1 - x1
28         2      y1 - z1
29         2      x1 - z1
30         2      z1 - x1}


Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.