Iterating through a column and mapping values

Viewed 81

Here is what I am trying to do. I want to substitute the values of this data frame. enter image description here

For example. Bernard to be substituted as 1, and then Drake as 2 and so on and so forth. How to iterate through the column to write a function that can do the following.

4 Answers

IIUC

data = {
    'Name' : ['My Name', 'My Name', 'Your Name', 'Your Name'],
    'Date' : ['2022-01-01', '2022-02-01', '2022-01-01', '2022-02-01']
}
df = pd.DataFrame(data)
df['Count'] = df.groupby(['Name']).cumcount() + 1
df

You can use the built in category codes to achieve this:

df.Name.astype('category').cat.codes+1

create a dictionary and the map

dict = {'bernard':1, 'drake':2, 'sansa':3}
df['code'] = df['name'].map(dict)
    name    date    code
0   bernard 01/11/2020  1
1   drake   01/11/2020  2
2   sansa   01/11/2020  3

is that what you're looking for?

The function already exists - pd.factorize.

It returns a tuple - first a new column with the values each item has been mapped to. Then second an index of the unique values.

df = pd.DataFrame({'name': ['Bernard', 'Bernard', 'Drake', 'Drake', 'Lance']})
pd.factorize(df.name)
(array([0, 0, 1, 1, 2]), Index(['Bernard', 'Drake', 'Lance'], dtype='object'))

Using that, we'd just assign a new column:

df = df.assign(codes=pd.factorize(df.name)[0] + 1)
df
      name  codes
0  Bernard      1
1  Bernard      1
2    Drake      2
3    Drake      2
4    Lance      3
Related