在没有Pandas的Python中用公共列连接两个CSV文件(内部连接)



我有两个文件:

数据.csv:

cod_pers, cod_enti, fec_venc
2317422,208,04/12/2022
2392115,210,04/02/2022
2086638,211,31/03/2022
2086638,212,03/13/2022

enti.csv:

cod_enti,cod_market
208,40
209,50
210,16
211,40
212,50

所以我想要一个包含以下内容的输出文件:

cod_pers, cod_enti, fec_venc, cod_market
2317422,208,04/12/2022,40
2392115,210,04/02/2022,16
2086638,211,31/03/2022,40
2086638,212,03/13/2022,50

我该怎么做?我已经写了下面的代码:

import csv
import numpy as np
from time import strftime
from datetime import datetime, date, time, timedelta
from dateutil.relativedelta import relativedelta
#Read the CSV file
str2date = lambda x: datetime.strptime(x, '%d/%m/%Y')
data_datos = np.genfromtxt(r'data.csv', delimiter=',', dtype=None, names=True, converters={'fec_venc':str2date}, encoding="UTF-8")
data_enti = np.genfromtxt(r'enti.csv', delimiter=',', dtype=None, names=True, encoding="UTF-8")
from numpy.lib import recfunctions
# your code to import csv
merged_array = recfunctions.join_by('cod_enti', data_datos, data_enti)

参见本文档

相关内容

  • 没有找到相关文章

最新更新