问题描述
employee id,employee name,city,state,salary
假设我想为 ex 应用一堆条件:如果城市是“多伦多”,则加薪 100。假设我有 100 个这样的规则,我必须写 100 个 if/else 条件。
将这些规则应用于 CSV 文件中的数据并更新文件的最有效方法是什么?
解决方法
您可以使用 pandas 来执行此操作
import pandas as pd
df = pd.read_csv('test.csv')
# if the city is "Toronto" increment salary by 100
df.loc[df['city'] == 'Toronto','salary'] += 100
# ...
# Other rules
df.to_csv('chaged.csv')