类型错误:"系列"对象是可变的,因此无法对其进行哈希处理



我知道这个错误很常见,我尝试了一些解决方案,但仍然不了解什么问题。我想这是由于行和行1的可变形式,但我无法弄清楚

我想做什么?我有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']]):

我通过更改循环找到了一种方法现在我的代码是:

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']):

我认为您正在迭代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

参考如何迭代数据帧:https://github.com/vi3k6i5/pandas_basics/blob/master/2.a teratiter erate Over A A A Dataframe.ipynb

我计时了您的索引方法和iTereratorows方法。这是结果:

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

简单地说iterrow的速度比您的方法快。

引用良好方法循环循环循环范围的循环框架的最有效方法是什么?

最新更新