Here is what I am trying to do. I want to substitute the values of this data frame.
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.
CodePudding user response:
Using that, we'd just assign a new column:
df = df.assign(codes=pd.factorize(df.name)[0] 1)
CodePudding user response:
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
CodePudding user response:
You can use the built in category codes to achieve this:
df.Name.astype('category').cat.codes 1
CodePudding user response:
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?