0

Is it possible to transform Table 1 to table 2, where a new multi-index table is created so that a specific 'class_section' grouping has its own index order? Is using group by required? or is there another way I can do this by split.apply.combine technique?

Thanks in advance.

Table 1

name movie_watch_count class_section
0 Morris 2 2
1 Gertie 3 1
2 Kristal 3 1
3 Roslyn 4 2
4 Tari 7 2
5 Dewayne 1 1
6 Edward 6 2
7 Frank 0 2

Table 2

global_index group_index name movie_watch_count class_section
0 1 0 Gertie 3 1
1 2 1 Kristal 3 1
2 5 2 Dewayne 1 1
3 0 0 Morris 2 2
4 3 1 Roslyn 4 2
5 4 2 Tari 7 2
6 6 3 Edward 6 2
7 7 4 Frank 0 2
FObersteiner
  • 22,500
  • 8
  • 42
  • 72

1 Answers1

0

For an independent indexing inside each group we can use groupby(...).cumcount():

from io import StringIO
import pandas as pd

data = '''  name    movie_watch_count   class_section
0   Morris  2   2
1   Gertie  3   1
2   Kristal     3   1
3   Roslyn  4   2
4   Tari    7   2
5   Dewayne     1   1
6   Edward  6   2
7   Frank   0   2'''

df = pd.read_csv(StringIO(data), sep=' \t', engine='python')
df['group_index'] = df.groupby('class_section').cumcount()
Vitalizzare
  • 4,496
  • 7
  • 13
  • 32