修正方法: 最初の引数は pandas オブジェクトの反復可能である必要があり、「dataframe」型のオブジェクトを渡しました。
Python の使用時に発生する可能性のある一般的なエラーは次のとおりです。
TypeError: first argument must be an iterable of pandas objects, you passed an object
of type "DataFrame"
このエラーは通常、 concat()関数を使用して、DataFrame 名をかっこで囲まずに 2 つの pandas DataFrame を追加しようとしたときに発生します。
次の例は、このエラーを実際に解決する方法を示しています。
エラーを再現する方法
次の 2 つのパンダ DataFrame があると仮定します。
import pandas as pd #create first DataFrame df1 = pd. DataFrame ({' x ': [25, 14, 16, 27, 20,15, 14], ' y ': [5, 7, 7, 5, 7, 6, 9], ' z ': [8, 8, 10, 6, 6, 9, 6]}) print (df1) X Y Z 0 25 5 8 1 14 7 8 2 16 7 10 3 27 5 6 4 20 7 6 5 15 6 9 6 14 9 6 #create second DataFrame df2 = pd. DataFrame ({' x ': [58, 60, 65], ' y ': [14, 22, 23], ' z ': [9, 12, 19]}) print (df2) X Y Z 0 58 14 9 1 60 22 12 2 65 23 19
ここで、 concat()関数を使用して 2 つの DataFrame を 1 つの DataFrame に追加しようとするとします。
#attempt to add two DataFrames together
combined = pd. concat (df1, df2, ignore_index= True )
#view final DataFrame
print (combined)
TypeError: first argument must be an iterable of pandas objects, you passed an object
of type "DataFrame"
concat()関数で DataFrame 名を括弧で囲むのに失敗したため、エラーが発生します。
エラーを修正する方法
このエラーを解決するには、次のようにconcat()関数で DataFrame 名を角かっこで囲むだけです。
#append two DataFrames together
combined = pd. concat ([df1, df2], ignore_index= True )
#view final DataFrame
print (combined)
X Y Z
0 25 5 8
1 14 7 8
2 16 7 10
3 27 5 6
4 20 7 6
5 15 6 9
6 14 9 6
7 58 14 9
8 60 22 12
9 65 23 19
今回はエラーなく 2 つの DataFrame を正常に結合できていることに注意してください。
追加リソース
次のチュートリアルでは、Python の他の一般的なエラーを修正する方法を説明します。
Python で修正する方法: オブジェクト ‘numpy.ndarray’ は呼び出し可能ではありません
修正方法: TypeError: オブジェクト ‘numpy.float64’ は呼び出し可能ではありません
修正方法: 型エラー: 文字列またはバイト オブジェクトが必要です