Reputation: 139
i have 2 data frames df1 has 3 columns cust_id,first name, last name df2 has 1 column cust_id
for each row in df2, i want to extract data from df1 and append to df3, df3 has 3 columns cust_id,first name, last name
i tried this
for i,r in df2.iterrows():
data = df1[df1[cust_id]==r]
pandas.concat(data,df3)
the above is giving me error: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"
i tried other as
for i,r in df2.iterrows():
data = df1[df1[cust_id]==r]
df3.append(data)
this code is creating a new dataframe other than df3
how can i achieve this without creating a new object?
Upvotes: 1
Views: 959
Reputation: 6483
The error couldn't be cleaner: As you can see in the docs pd.concat
receives an iterable and other aditional parameters, but you are passing a dataframe(data
) as first parameter and a another dataframe(df3
) as another parameter, instead of an iterable with the dataframes to concat, so you should change pandas.concat(data,df3)
to pandas.concat([data,df3])
. And as a suggestion, to avoid the usage of iterrows
, you can try with pd.Series.isin
:
pd.concat([df1[df1['cust_id'].isin(df2['cust_id'])],df3])
Upvotes: 1