Python(CSV/XLSX编辑)- 如果'A'列包含"string",则在B列中写入"string_2"
Python (CSV/XLSX Editing) - If Column 'A' includes "string", then write "string_2" in Column B
当前结果:
*Email* *Organization*
m.ali@firstdomain.com /Org: First
m.tyson@seconddomain.com /Org: First
e.holyfield@firstdomain.com /Org: First
期望的结果:
*Email* *Organization*
m.ali@firstdomain.com /Org: First
m.tyson@seconddomain.com /Org: Second
e.holyfield@firstdomain.com /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')
有什么办法吗?目前正在与 pandas 合作修改此 csv。
我使用以下方法将一组域与另一组域分开:
df = pd.read_csv("file.csv", sep=r'\s*,\s*', engine='python')
second_domains = df.loc[df['Email'].str.contains('second')]
但我不知道还能做什么。
非常感谢大家!
尝试使用 loc
分配:
df.loc[df['Email'].str.contains('second'), 'Organization'] = '/Org: Second'
当前结果:
*Email* *Organization*
m.ali@firstdomain.com /Org: First
m.tyson@seconddomain.com /Org: First
e.holyfield@firstdomain.com /Org: First
期望的结果:
*Email* *Organization*
m.ali@firstdomain.com /Org: First
m.tyson@seconddomain.com /Org: Second
e.holyfield@firstdomain.com /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')
有什么办法吗?目前正在与 pandas 合作修改此 csv。 我使用以下方法将一组域与另一组域分开:
df = pd.read_csv("file.csv", sep=r'\s*,\s*', engine='python')
second_domains = df.loc[df['Email'].str.contains('second')]
但我不知道还能做什么。
非常感谢大家!
尝试使用 loc
分配:
df.loc[df['Email'].str.contains('second'), 'Organization'] = '/Org: Second'