使用“ pd.merge”合并时间数据时发生错误

时间:2019-01-20 03:51:09

标签: python pandas csv numpy

我目前正在尝试使用以下代码合并两个csv文件:

import pandas as pd
data1 = pd.read_csv("QLD.csv") 
data2 = pd.read_csv("VIC.csv")
result = pd.merge(data1[['REGION', 'TOTALDEMAND', 'RRP']], data2[['REGION', 'TOTALDEMAND', 'RRP']], on='SETTLEMENTDATE')
result.to_csv("masterfile.csv", index=False)

这是我的每个csv文件的头的样子: QLD VIC

运行代码时,我收到此错误:

Traceback (most recent call last):
  File "/Users/george/Desktop/collate/asdas.py", line 4, in <module>
    result = pd.merge(data1[['REGION', 'TOTALDEMAND', 'RRP']], data2[['REGION', 'TOTALDEMAND', 'RRP']], on='SETTLEMENTDATE')
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/reshape/merge.py", line 61, in merge
    validate=validate)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/reshape/merge.py", line 551, in __init__
    self.join_names) = self._get_merge_keys()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/reshape/merge.py", line 857, in _get_merge_keys
    rk, stacklevel=stacklevel))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/generic.py", line 1382, in _get_label_or_level_values
    raise KeyError(key)
KeyError: 'SETTLEMENTDATE'

有什么想法吗?谢谢

edit1: settlement data centre

2 个答案:

答案 0 :(得分:1)

您希望在'SETTLEMENTDATE'上进行合并,但是从您的输入数据框中排除了该系列。因此,Pandas筹集了KeyError。请尝试:

result = pd.merge(data1[['REGION', 'TOTALDEMAND', 'RRP', 'SETTLEMENTDATE']],
                  data2[['REGION', 'TOTALDEMAND', 'RRP', 'SETTLEMENTDATE']],
                  on='SETTLEMENTDATE')

答案 1 :(得分:1)

尝试:

result = pd.merge(data1[['REGION', 'TOTALDEMAND', 'RRP','SETTLEMENTDATE']], data2[['REGION', 'TOTALDEMAND', 'RRP','SETTLEMENTDATE']], on='SETTLEMENTDATE')

您正在SETTLEMENTDATE上进行合并,但不会在要合并的数据帧切片中进行合并。