Perform operations over a list of dataframes

Viewed 177

I wrote a script with one dataframe df1 and applied to it a lot of operations. For example:

df1 = df1.rename(columns={'Unnamed: 0': ''})
df1.iloc[:,0] = re.search('\d{2}', df1.columns[3]).group(0)

But now I have a list of dataframes dfs. Is it possible, and so how can I apply all operations to a list of dataframes?

I tried

for df in dfs:
    df = df.rename(columns={'Unnamed: 0': ''})
    df.iloc[:,0] = re.search('\d{2}', df.columns[3]).group(0)

But it is overwriting and I get only the last dataframe from the list.

1 Answers

Before you start the loop, you initialize an empty df_total. At the end of each loop, you concatenate the current df to the df_total, so that this last grows up each loop, saving your previous dataframes. An example of code:

df_total = pd.DataFrame()

for df in dfs:
    df = df.rename(columns={'Unnamed: 0': ''})
    df.iloc[:,0] = re.search('\d{2}', df.columns[3]).group(0)
    df_total = pd.concat([df_total , df], ignore_index = True)
Related