【发布时间】:2020-01-28 22:06:39
【问题描述】:
【问题讨论】:
-
您的问题需要更多的细节和解释,否则恐怕会被关闭。此外,您不应将数据/代码粘贴为图片,以文本格式提供,以便人们可以复制并尝试自己重现您的问题。
标签: python pandas dataframe variables
【问题讨论】:
标签: python pandas dataframe variables
您可以尝试以下方法:
# create a mapping for artist to a number
maps = {ar:en for en, ar in enumerate(df['artist'].unique())}
df['artist_code'] = df['artist'].map(maps)
artist artist_code
0 a 0
1 a 0
2 a 0
3 b 1
4 c 2
样本数据
df = pd.DataFrame({'artist':['a','a','a','b','c']})
【讨论】:
IIUC,使用Series.factorize
df['artist_code']=df['artis'].factorize()[0]
df['artist_code'] = df.groupby('artist').ngroup()
# artist artist_code
#0 a 0
#1 a 0
#2 a 0
#3 b 1
#4 c 2
【讨论】: