【发布时间】:2021-12-24 15:26:36
【问题描述】:
这是我的代码:
import pandas as pd
import re
# reading the csv file
patients = pd.read_csv("partial.csv")
# updating the column value/data
for patient in patients.iterrows():
cip=patient['VALOR_ID']
new_cip = re.sub('^(\w+|)',r'FIXED_REPLACED_STRING',cip)
patient['VALOR_ID'] = new_cip
# writing into the file
df.to_csv("partial-writer.csv", index=False)
print(df)
我收到了这条消息:
Traceback(最近一次调用最后一次): 文件“/home/jeusdi/projects/workarea/salut/load-testing/load.py”,第 28 行,在 cip=患者['VALOR_ID'] TypeError: 元组索引必须是整数或切片,而不是 str
编辑 上面的表单代码你可以认为我需要为所有行设置一个相同的固定值。
我需要遍历“行”并生成一个随机字符串并将其设置在每个不同的“行”上。
上面的代码是:
for patient in patients.iterrows():
new_cip = generate_cip()
patient['VALOR_ID'] = new_cip
【问题讨论】:
-
你能添加一些数据样本来提问吗?
标签: python-3.x pandas csv