Tibor Udvari
Tibor Udvari

Reputation: 3012

How to index unique combination of values with pandas

I am working with school schedule data and I have to differentiate different sessions of the same course.

If a different class has the same course, this is in effect another session of the same course and needs to be differentiated. This means having an extra column with a session index.

import pandas as pd

cols = ['course', 'class_name', 'professor']

data = [ ['Math',    'X', 'Bob'],
         ['Math',    'X', 'Bob'], 
         ['Math',    'Y', 'Bob'],
         ['English', 'Y', 'Tim'],
         ['English', 'X', 'Jim'],
         ['English', 'X', 'Jim'],
       ]

df = pd.DataFrame(columns=cols, data=data)

# Add session
df['session'] = '?'
print(df)

The result should be something like this.

    course  class_name  professor   session
0   Math    X   Bob     0
1   Math    X   Bob     0
2   Math    Y   Bob     1
3   Eng.    Y   Tim     1
4   Eng.    X   Jim     0
5   Eng.    X   Jim     0

I have come up with a convoluted procedural solution, what would be a more pandas way of doing this?

groups = df.groupby(['course', 'class_name'])

d_sessions = {}
counter = 0
pclass = ""
pcourse = ""

for m_idx in list(groups.groups):
    course = m_idx[0]
    class_ = m_idx[1]

    if class_ != pclass:
        counter += 1

    if pcourse != course:
        counter = 0

    pclass = class_
    pcourse = course    
    d_sessions[m_idx] = counter

df.set_index(['course', 'class_name'], inplace=True)

for k, v in d_sessions.items():
    df.set_value(col='index', value=v, index=k)

df.reset_index(inplace=True)
df

Upvotes: 3

Views: 1703

Answers (2)

Scott Boston
Scott Boston

Reputation: 153500

Let's try:

df['session'] = df.groupby('course')['class_name'].transform(lambda x: (~x.duplicated()).cumsum())

Output:

    course class_name professor  session
0     Math          X       Bob        1
1     Math          X       Bob        1
2     Math          Y       Bob        2
3  English          Y       Tim        1
4  English          X       Jim        2
5  English          X       Jim        2

Upvotes: 3

Steven
Steven

Reputation: 649

interesting question.

Try making a map with all the unique combinations and their session number.

Import data in df (note that I added some rows to validate my solution):

import pandas as pd

cols = ['course', 'class_name', 'professor']

data = [ ['Math',    'X', 'Bob'],
         ['Math',    'X', 'Bob'],
         ['Math',    'X', 'Bob'], 
         ['Math',    'Y', 'Bob'],
         ['English', 'Y', 'Tim'],
         ['English', 'X', 'Jim'],
         ['English', 'X', 'Jim'],
         ['English', 'Z', 'Mark'],
         ['Chinese', 'X', 'Mark'],
         ['Chinese', 'X', 'Mark'],
         ['Chinese', 'F', 'Mark'],
       ]

df = pd.DataFrame(columns=cols, data=data)

Then create a map for every unique course-class combination and give them session numbers. The way I'm doing this, is slicing the frame by course and then find the unique class values for this course. Then create a unique name for each combination and give each session a unique session number (for every course). Then overlay the session column with the dictionary:

def sol():
    map = {}

    for item in df.course.unique():
        slice = df[df['course'] == item]
        mapslice = dict(zip(item + slice.class_name.unique(), 
        list(range(len(slice.class_name.unique())))))
        map.update(mapslice)

    df['session'] = (df.course + df.class_name).map(map)

    return df

This returns for the sample data:

course  class_name  professor   session
0   Math    X   Bob 0
1   Math    X   Bob 0
2   Math    X   Bob 0
3   Math    Y   Bob 1
4   English Y   Tim 0
5   English X   Jim 1
6   English X   Jim 1
7   English Z   Mark    2
8   Chinese X   Mark    0
9   Chinese X   Mark    0
10  Chinese F   Mark    1

Then a quick performance check:

%timeit sol()

3.66 ms ± 44 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Compared to your solution (here called ori):

%timeit ori()
4.4 ms ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

Thanks for letting me know if this answer was helpful.

Upvotes: 1

Related Questions