这是我的密码:
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)我收到这样的信息:
"/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发布于 2021-11-12 09:01:56
使用Series.str.replace,但不能确定正则表达式中的|。也许应该移除它:
df = pd.read_csv("partial.csv")
df['VALOR_ID'] = df['VALOR_ID'].str.replace('^(\w+|)',r'FIXED_REPLACED_STRING')
#if function return scalars
df['VALOR_ID'] = df['VALOR_ID'].apply(generate_cip)
df.to_csv("partial-writer.csv", index=False)https://stackoverflow.com/questions/69940233
复制相似问题