Pandas KeyError,同时从另一个DataFrame添加多个列

时间:2019-02-04 13:31:55

标签: python pandas jupyter-notebook keyerror

我有大量积压的csv。我想从中获取一些列并将其添加到另一个DataFrame中。看起来像这样:

Backlog = r'C:\Users\Desktop\personalized reports\Backlog Case\R1_Ext_Report.csv'
df_backlog = pd.read_csv(Backlog, sep=',')
df2 = df_backlog['PROMOTION CODE', 'MODEL DESCRIPTION']

以上各列的名称与csv中的名称完全相同。如果我仅用一列(例如“促销代码”)运行代码,则该代码可以使用,但是一旦添加了两个或多个代码,就会收到KeyError:

KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('PROMOTION CODE', 'MODEL DESCRIPTION')

将感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您需要为多个列提供列表

name: "basic-network"

x-type: "hlfv1"

description: "The basic network"

version: "1.0"

channels:
# name of the channel
bimtrustchannel1:

orderers:
  - orderer.BIMTrust.com
# Required. list of peers from participating orgs
peers:
  peer0.org1.BIMTrust.com:
     endorsingPeer: true
     eventSource: true

organizations:
  Org1:
    mspid: Org1MSP
    peers:
  - peer0.org1.BIMTrust.com

certificateAuthorities:
  - ca-org1

orderers:
  orderer.BIMTrust.com:
    url: grpc://localhost:7050

grpcOptions:
  ssl-target-name-override: orderer.BIMTrust.com

peers:
peer0.org1.BIMTrust.com:
# this URL is used to send endorsement and query requests
url: grpc://localhost:7051
grpcOptions:
  ssl-target-name-override: peer0.org1.BIMTrust.com
  request-timeout: 120001

certificateAuthorities:
  ca-org1:
    url: http://localhost:7054

httpOptions:
  verify: false

registrar:
  - enrollId: admin
    enrollSecret: adminpw

caName: ca-org1