TypeError: ออบเจ็กต์ 'ซีรี่ส์' นั้นไม่แน่นอน ดังนั้นจึงไม่สามารถแฮชได้

ฉันรู้ว่าข้อผิดพลาดนี้เป็นเรื่องปกติ ฉันลองวิธีแก้ปัญหาบางอย่างแล้วค้นหาแล้ว แต่ก็ยังไม่เข้าใจว่ามีอะไรผิดปกติ ฉันเดาว่ามันเป็นเพราะรูปแบบที่ไม่แน่นอนของ row และ row1 แต่ฉันคิดไม่ออก

ฉันกำลังพยายามทำอะไร? ฉันมี 2 ดาต้าเฟรม ฉันต้องวนซ้ำแถวของ 1 แถวแรก และสำหรับแต่ละบรรทัดของแถวแรก ให้วนซ้ำแถวที่สองและตรวจสอบค่าของเซลล์สำหรับบางคอลัมน์ รหัสของฉันและความพยายามที่แตกต่างกัน:

a=0
b=0
  for row in Correction.iterrows():
        b+=1
        for row1 in dataframe.iterrows():
            c+=1
            a=0
            print('Handling correction '+str(b)+' and deal '+str(c))
            if (Correction.loc[row,['BO Branch Code']]==dataframe.loc[row1,['wings Branch']] and Correction.loc[row,['Profit Center']]==dataframe.loc[row1,['Profit Center']] and Correction.loc[row,['Back Office']]==dataframe.loc[row1,['Back Office']]
                and Correction.loc[row,['BO System Code']]==dataframe.loc[row1,['BO System Code']]):

ฉันยังพยายาม

a=0
b=0
 for row in Correction.iterrows():
        b+=1
        for row1 in dataframe.iterrows():
            c+=1
            a=0
            print('Handling correction '+str(b)+' and deal '+str(c))
            if (Correction[row]['BO Branch Code']==dataframe[row1]['wings Branch'] and Correction[row]['Profit Center']==dataframe[row1]['Profit Center'] and Correction[row]['Back Office']==dataframe[row1]['Back Office']
                and Correction[row]['BO System Code']==dataframe[row1]['BO System Code']):

และ

a=0
b=0
 for row in Correction.iterrows():
        b+=1
        for row1 in dataframe.iterrows():
            c+=1
            a=0
            print('Handling correction '+str(b)+' and deal '+str(c))
            if (Correction.loc[row,['BO Branch Code']]==dataframe[row1,['wings Branch']] and Correction[row,['Profit Center']]==dataframe[row1,['Profit Center']] and Correction[row,['Back Office']]==dataframe[row1,['Back Office']]
                and Correction[row,['BO System Code']]==dataframe[row1,['BO System Code']]):

person Mayeul sgc    schedule 28.02.2017    source แหล่งที่มา


คำตอบ (2)


ฉันพบวิธีโดยการเปลี่ยน for loop ตอนนี้รหัสของฉันคือ:

a=0
b=0
 for index in Correction.index:
        b+=1
        for index1 in dataframe.index:
            c+=1
            a=0
            print('Handling correction '+str(b)+' and deal '+str(c))
            if (Correction.loc[row,'BO Branch Code']==dataframe.loc[row1,'Wings Branch]] and Correction.loc[row,'Profit Center']==dataframe.loc[row1,'Profit Center'] and Correction.loc[row,'Back Office']==dataframe.loc[row1,'Back Office']
                and Correction.loc[row,'BO System Code']==dataframe.loc[row1,'BO System Code']):
person Mayeul sgc    schedule 28.02.2017

ฉันคิดว่าคุณกำลังทำซ้ำ df ของคุณผิด

for row in Correction.itertuples():
    bo_branch_code = row['BO Branch Code']
    for row1 in dataframe.itertuples():
        if row1['wings Branch'] == bo_branch_code:
            # do stuff here

อ้างอิงถึงวิธีการวนซ้ำ DataFrame: https://github.com/vi3k6i5/pandas_basics/blob/master/2.A%20Iterate%20over%20a%20dataframe.ipynb

ฉันกำหนดเวลาแนวทางดัชนีของคุณและวนซ้ำแนวทาง นี่คือผลลัพธ์:

import pandas as pd
import numpy as np
import time

df = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))

df_2 = pd.DataFrame(np.random.randint(0,100,size=(10, 4)), columns=list('ABCD'))

def test_time():
    for index in df.index:
        for index1 in df_2.index:
            if (df.loc[index, 'A'] == df_2.loc[index1, 'A']):
                continue

def test_time_2():
    for idx, row in df.iterrows():
        a_val = row['A']
        for idy, row_1 in df_2.iterrows():
            if (a_val == row_1['A']):
                continue

start= time.clock()
test_time()
end= time.clock()
print(end-start)
# 0.038514999999999855

start= time.clock()
test_time_2()
end= time.clock()
print(end-start)
# 0.009272000000000169

แค่พูดว่า iterrows ก็เร็วกว่าแนวทางของคุณมาก

การอ้างอิงถึงแนวทางที่ดีในการวนซ้ำดาต้าเฟรม วิธีใดที่มีประสิทธิภาพมากที่สุดในการวนซ้ำดาต้าเฟรมด้วยแพนด้า?

person Vikash Singh    schedule 28.02.2017