1

I trying to loop a function for each column to transform it and concatenate it back to the main data frame. But I am only getting the last loop output.

import pandas as pd

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))

columns = ['A','B']

def run_all(x):
    x = x*2
    return x

df_summary = pd.DataFrame()

for column in columns:
    df0 = df[column]
    df__ = []
    for each_row in df0:
        df__.append(run_all(each_row))
    df_summary  = pd.concat([df, pd.DataFrame({'Summary_' + column: df__})], axis=1)

df_final = pd.DataFrame(df_summary)

print(df_final)
  • Of course. `df_summary` is a variable local to the for loop so it's not updating anything outside the for loop. And as a result you only get the last result. Try adding `df_summary = pd.DataFrame()` before the start of the loop. – NotAName Apr 15 '21 at 23:35
  • Still getting same result. – satya kapoor Apr 15 '21 at 23:45
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Show where the intermediate results differ from what you expected. – Prune Apr 15 '21 at 23:51
  • Your posted code dies due to private input. Please [include a minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of your MRE. – Prune Apr 15 '21 at 23:51
  • I have edited the query as requested. – satya kapoor Apr 16 '21 at 00:09

1 Answers1

1

Do you want to try the following:-

df_summary = df.copy()

for column in columns:
    df0 = df[column]
    df__ = []
    for each_row in df0:
        df__.append(run_all(each_row))
    df_summary['Summary_{}'.format(column)] = pd.DataFrame({'Summary_' + column: df__}).values

print(df_summary)

output:

enter image description here

hongkail
  • 679
  • 1
  • 10
  • 17