![](/img/trans.png)
[英]groupby comma-separated values in single DataFrame column python/pandas
[英]Python Dataframe replace multiple values that separated by comma single row
根據您的描述,我已經創建了示例數據並嘗試做您想做的事情。
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)
輸出:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.