JackLidge
JackLidge

Reputation: 441

more pythonic way to concatenate pandas data frames

So I've been having to write programs which do something to an existing pandas data frame and then at that data frame to the end of a big data frame in a for loop.

I've found a way to do this, by setting the first data frame to be the end data frame for the first iteration, and then concatenating data frames to this end data frame in later iterations but it doesn't seem to be the most efficient way to do this to me.

I've been using python for a while but have only recently started using pandas so I don't know if there is an easier way to do this. I've attached a simple sample code which hopefully demonstrates what I'm doing and was wondering whether it can be done more pythonically.

df = pandas.DataFrame([0,1,2,3])
for i in range(3):
    if i == 0:
        end_df = df
    else:
        end_df = pandas.concat([end_df,df],ignore_index=True)

Upvotes: 0

Views: 68

Answers (1)

Marcin
Marcin

Reputation: 4323

If you want to have just one variable, you can simplify your code:

df = pd.DataFrame([0,1,2,3])
for i in range(3):
    df =  pd.concat([df, some_op_modifying_df(df)], ignore_index=True)

, where some_op_modifying_df is a function that generates a new version of df.

Having said that, this would be way easier to come up with some sensible solution if you provided more detail of your problem.

Upvotes: 1

Related Questions