使用pandas为公共变量合并数据集

时间:2017-10-24 18:17:07

标签: python pandas merge

我尝试使用公共变量合并数据集, 但因为var名称很复杂,只是在csv文件中更改。

现在我需要合并,但var名称可能无法识别 KeyError:'EID'

我该怎么办?

DATA1:

Itv Schedule, Data: Itv Session,    EID,    Employer: Organization Name,
Fall 2015, CS,  9b3000a21d0fbc59e648dd7eb61265cb,   Alibaba,
Fall 2015, CS,  3e14213c3e34a38c1e6fa8b9577ecf81,   Amazon,
Fall 2015, CS,  0b87a844f4c607d532b3ee7f4cbae22a,   Credit,
Fall 2015, CS,  9f694bd711a347c2c0177a06505c2462,   Corporation,
Fall 2015, CS,  c41ad017309167498e43592ea15273a4,   Financial,

DATA2:

Itv Schedule, Data: Itv Session,    EID,    Employer: Organization Name,
Fall 2016, CS,  94ac45de9cd4847991aab265c750921a,   CS,
Fall 2016, CS,  62b139cfa0b668c7f934f7001a999be6,   IBM,
Fall 2016, CS,  224c539042211ba53add9cc831bc8a33,   Petrol,
Fall 2016, CS,  5f32811c9de563cfe8328847d5d3c180,   Stock,

DATA3:

Itv Schedule, Data: Itv Session,    EID,    Employer: Organization Name,
Fall 2017, SAM, 0a1f9785f5c111c373b5034c296c37b3,   Honest,
Fall 2017, SAM, 9f694bd711a347c2c0177a06505c2462,   Corporation,
Fall 2017, CS,  5f32811c9de563cfe8328847d5d3c180,   Stock,

我的代码:

import pandas as pd
import numpy as np

data1516 = pd.read_csv('data1516.csv', sep='|', names=None, header=0, encoding='latin-1')
data1617 = pd.read_csv('data1617.csv', sep='|', names=None, header=0, encoding='latin-1')
data1718 = pd.read_csv('data1718.csv', sep='|', names=None, header=0, encoding='latin-1')


N=[data1516,data1617,data1718]
import functools 
df=functools.reduce(lambda x, y: pd.merge(x, y, on = "EID", how='outer'), N)

#df = pd.merge(data1516, data1617, on='EID', how='outer')

想要合并而不是做concat

0 个答案:

没有答案