Python (CSV/XLSX Editing) – If Column 'A' includes "string", then write "string_2" in Column B


Current outcome:

       *Email*                  *Organization*
[email protected]            /Org: First
[email protected]         /Org: First
[email protected]      /Org: First

Desired outcome:

       *Email*                  *Organization*
[email protected]            /Org: First
[email protected]         /Org: Second
[email protected]      /Org: First


If a row in column('Email') contains the string('second'):
Replace the string in the same row, under column('Organization') with the string('/Org: Second')

Any way of doing this? Currently working with pandas to amend this csv.
I used the following to segregate one set of domains from the other:

df = pd.read_csv("file.csv", sep=r'\s*,\s*', engine='python')
second_domains = df.loc[df['Email'].str.contains('second')]

But I don’t know what else to do from here.

Thank you all very much!


Try using loc assignment:

df.loc[df['Email'].str.contains('second'), 'Organization'] = '/Org: Second'

Answered By – U12-Forward

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published