python处理一个csv数据,根据其中一列数据来得到新一列写入同一个文件中?

2025-03-24 12:33:29
推荐回答(2个)
回答1:

用pandas库可以实现:

import pandas as pd
file = 'your_csv_path.csv'
data = pd.read_csv(file)

data['sort'] = data['gender'].map(lambda x: 1 if x == 'man' else -1)
data.to_csv(file, index=False)

回答2: