如何从当前表中创建另一列python pandas



我有下表:

第C列>类型B[/tr>
第A列 第B列
1 AB001 类型A
2 AB012 类型A
3 AB035
4 AB039 类型B
5 AB065 类型A
6 AB088 类型B

通过dictionary对类型字符串使用Series.map,并通过+:连接在一起

#if no match `TYPE-A` or `TYPE-B` added default value no match
s = df['ColumnC'].map({'TYPE-A':'abc','TYPE-B':'xyz'}).fillna('no match')
df['ColumnD'] = ' www.website.com/' + s + '/' + df['ColumnB'].astype(str)
print (df)
ColumnA ColumnB ColumnC                     ColumnD
0        1   AB001  TYPE-A   www.website.com/abc/AB001
1        2   AB012  TYPE-A   www.website.com/abc/AB012
2        3   AB035  TYPE-B   www.website.com/xyz/AB035
3        4   AB039  TYPE-B   www.website.com/xyz/AB039
4        5   AB065  TYPE-A   www.website.com/abc/AB065
5        6   AB088  TYPE-B   www.website.com/xyz/AB088

最新更新