Pandas, build a new dataframe with a for loop

I have a really simple problem that I cannot solve in Pandas. I have a data frame from which I want to apply some function. I want to repeat this many times and build / collect the results from operations in a new, larger data framework. I thought about this with a for loop. Here is a simplified example that I cannot work with:

import pandas as pd

df = pd.DataFrame(np.random.randn(3, 4), columns=list('ABCD'))

large_df = df*0

for i in range(1,10):
    df_new = df*i
    large_df= pd.concat(large_df,df_new)

large_df

Any ideas?

+4
source share
1 answer

The fastest creation of all results at first and concatenation once at the end. If you add one result at a time, the memory for the results must be reallocated every time.

, some_function p (, i ), :

pd.concat([df.apply(lambda x: some_function(x, p)) for p in parameters])
+5

Source: https://habr.com/ru/post/1532811/


All Articles