2

I am building one "master" dataframes by concatenating in a for loop

df = pd.concat([df, pd.DataFrame(listdf)], axis = 1)

listdf is the temp list for preprocessing before it is concatenated to the dataframe. The problem I am facing is that as the number of iterations of the loop increases, process of building the dataframe gets slower and slower.

I have researched here and based on this suggestion, it is much faster to build a list and then to finally concatenate it into a dataframe Why does concatenation of DataFrames get exponentially slower?

However this approach is not working

interimdf.append(listdf)
df = pd.concat(interimdf)

I am getting this error

TypeError: cannot concatenate object of type '<class 'list'>'; only Series and DataFrame objs are valid

How do I resolve this issue. Also there is an alternative way to speed up the process to create one big dataframe in a loop

Bappa
  • 71
  • 6

1 Answers1

1

You need to append dataframes to your list and not the data as list.

Try:

interimdf.append(pd.DataFrame(listdf))

Then outside of your loop,

pd.concat(interimdf) 
Scott Boston
  • 147,308
  • 15
  • 139
  • 187