Search code examples
pythonpandasdataframecountconcatenation

Concatenate count variable and string in python


Code :

import pandas as pd
name = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
age = ['1', '1', '2', '1', '3', '2', '3', '2', '1']
list_of_tuples = list(zip(name, age))
list_of_tuples
count = []
df = pd.DataFrame(list_of_tuples, columns=['name', 'age'])
df['count'] = (df.groupby(['age']).cumcount() + 1)
print(df)

the current output is this:

  name age  count
0    a   1      1
1    b   1      2
2    c   2      1
3    d   1      3
4    e   3      1
5    f   2      2
6    g   3      2
7    h   2      3
8    i   1      4

But i need output like below

   name  age   count
0    a   1      cat_1
1    b   1      cat_2
2    c   2      rat_1
3    d   1      cat_3
4    e   3      mat_1
5    f   2      rat_2
6    g   3      mat_2
7    h   2      rat_3
8    i   1      cat_4

Can anyone help me how to do? And explain how you solved it?


Solution

  • Use Series.map by dictionary and add counts converted to strings:

    d = {'1':'cat', '2':'rat', '3':'mat'}
    #if numbers are integers
    #d = {1:'cat', 2:'rat', 3:'mat'}
    df['count'] = df['age'].map(d) + '_' +(df.groupby(['age']).cumcount() + 1).astype(str)
    
    print (df)
      name age  count
    0    a   1  cat_1
    1    b   1  cat_2
    2    c   2  rat_1
    3    d   1  cat_3
    4    e   3  mat_1
    5    f   2  rat_2
    6    g   3  mat_2
    7    h   2  rat_3
    8    i   1  cat_4