简体   繁体   中英

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

I have the following CSV file output:

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

The goal at hand is to append customer1 the string -business only if the word is present in the same row under Misc .

That is, I am looking for the final CSV output to be:

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

Essentially, the keyword here is business . I need to make sure customer1 and customer1-business are treated as separate customers despite them sharing the same name customer1 .

Thoughts?

We can do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM