我有如下数据集:
ID Type
1 a
2 a
3 b
4 b
5 c
我正试图创建列URL,如图所示,方法是根据“Type”指定不同的URL并附加“ID”
ID Type URL
1 a http://example.com/examplea/id=1
2 a http://example.com/examplea/id=2
3 b http://example.com/bbb/id=3
4 b http://example.com/bbb/id=4
5 c http://example.com/testc/id=5
我在代码中使用了类似的东西,但它并不是只为那一行引入ID,而是附加所有Type=a的ID
df.loc[df['Type'] == 'a', 'URL']= 'http://example.com/examplea/id='+str(df['ID'])
df.loc[df['Type'] == 'b', 'URL']= 'http://example.com/bbb/id='+str(df['ID'])
您应该稍微修改一下命令:
或者您可以像这样使用
map
:输出:
相关问题 更多 >
编程相关推荐