Python Dataframe replace multiple values that separated by comma single row

Based on your description, I have created sample data and tried to do what you are willing to.

df = pd.DataFrame()
df['academic_id'] = ['1','1,2,3','3,4','5,6']

# created a dictionary with the mappings we need
info = {'1':'course a','2':'course b', '3':'course c', '4':'course d','5':'course e', '6':'course f'}

def mapper(elem):
    return ','.join([info[i] for i in elem.split(',')])

df['academic_id_text'] = df['academic_id'].apply(mapper)
print(df)

Output:

    academic_id academic_id_text
0             1 course a
1         1,2,3 course a,course b,course c
2           3,4 course c,course d
3           5,6 course e,course f

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top