【发布时间】:2021-08-11 14:40:51
【问题描述】:
我需要一些帮助:
如何使用 Pandas 和 Python 更新文件 file_csv_reference.csv dataFrame 的列?
file_csv_reference.csv:
cod_example
123456
123456
123456
789101
789101
121314
121314
有重复信息的行,我想用下面文件中相应的更新代码替换它们:
file_with_updated_cod.csv
old_cod updated_cod
123456 ;1234567
789101 ;7891011
121314 ;1213141
到目前为止,我一直在考虑这种方式(但我无法正确运行):
import pandas as pd
file01 = pd.read_csv("file_csv_reference.csv", encoding = "utf-8", delimiter = ";", header = 0)
file02 = pd.read_csv("file_with_updated_cod.csv", encoding = "utf-8", delimiter = ";", header = 0)
for oldcod in file01['cod_example']:
for cod in file02['old_cod']:
if oldcod == cod:
#in this part I would like to replace the data in the file01 column cod_example
# with file01['updated_cod'] in the respective column
你能帮我解决这个问题吗?谢谢!
【问题讨论】:
标签: python pandas dataframe csv