2017-02-28 4 views
0

このエラーはよくあることですが、いくつかの解決策を試しましたが、何が間違っているのか分かりません。私は行と行1の変更可能なフォームのためだと思うが、私はそれを把握することはできませんTypeError: 'Series'オブジェクトは変更可能であるため、ハッシュすることはできません

私は何をしようとしていますか? 私は2つのデータフレームを持っています。私は最初の1の行を繰り返し処理し、最初の行の各行について2番目の行を繰り返し、いくつかの列のセルの値をチェックする必要があります。 私のコードと異なるの試み:

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

答えて

0

を試してみました、私はループ のための私を変更することで回避する方法を発見した今、私のコードは次のとおりです。

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']): 
0
私はあなたのインデックスのアプローチとiteraterowsアプローチを時限 https://github.com/vi3k6i5/pandas_basics/blob/master/2.A%20Iterate%20over%20a%20dataframe.ipynb

私は

は、データフレームを反復処理する方法を参照
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 

あなたのDFが間違って反復されていると思います。結果は次のとおりです。

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はあなたのアプローチよりも速いです。

データフレームでのループの良いアプローチに関する参考資料What is the most efficient way to loop through dataframes with pandas?

関連する問題