如何在 Python 中将数据从一个 CSV 列附加到另一列

How to append data from one CSV column to another in Python

我有以下 CSV 文件输出:

Customer,Misc
customer1,business-dns-test2
customer2,dns-test2
customer1,dns-test1

目前的目标是仅当单词出现在 Misc.

下的同一行中时才附加 customer1 字符串 -business

也就是说,我正在寻找最终的 CSV 输出:

Customer,Misc
customer1-business,dns-test2
customer2,dns-test2
customer1,dns-test1

本质上,这里的关键字是business。我需要确保 customer1customer1-business 被视为不同的客户,尽管他们共享相同的名称 customer1.

想法?

我们可以做到

df.loc[df.Misc.str.startswith('business'),'Customer']+='-business'
df.Misc=df.Misc.str.strip('business-')
df
Out[93]: 
             Customer       Misc
0  customer1-business  dns-test2
1           customer2  dns-test2
2           customer1  dns-test1