more pythonic way to concatenate pandas data frames

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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)





What's wrong with end_df = pandas.concat([df, df2], ignore_index=True) ?
– jpp
Aug 9 at 14:07



end_df = pandas.concat([df, df2], ignore_index=True)





Had a look over what I'm trying to do and I guess it's more a case of having an iterating dataframe, df, which is then different each time, so only one data frame variable each iteration. Will edit the original post for clarity.
– JackLidge
Aug 10 at 8:07




1 Answer
1



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.


some_op_modifying_df


df



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





I'm a fan of df.pipe(some_op_modifying_df). But, yes, this seems the most appropriate way.
– jpp
Aug 10 at 10:50


df.pipe(some_op_modifying_df)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS