3

I have the following loop which generates multiple rows based on the input dataframe:

for _, value in df.iterrows():

    var1 = value['Sales']
    var2 = value['Income']

    seg1 = value['segment']

    flag1 = 'up' if var1>0 else 'down'
    flag2 = 'up' if var2>0 else 'down'

    print(f"{seg1} Sales {flag1} {var1}% vs LY while Total income {flag2} {var2}% vs LY creating leverage")

OUTPUT

A Sales up 184.37% vs LY while Total income up 224.24% vs LY creating leverage
B Sales up 45.42% vs LY while Total income up 176.79% vs LY creating leverage

Is there a way to create a new dataframe from the output lines generated from the above loop.

Expected output1:

df:
     String
0    A Sales up 184.37% vs LY while Total income up 224.24% vs LY creating leverage
1    B Sales up 45.42% vs LY while Total income up 176.79% vs LY creating leverage

Expected output2:

df:
     String
0    A Sales up 184.37% vs LY while Total income up 224.24% vs LY creating leverage; B Sales up 45.42% vs LY while Total income up 176.79% vs LY creating leverage

I have tried the following approach but its either incorrect or has a wrong syntax:

column_names = ['String']
df= pd.DataFrame(columns = column_names)
df= pd.DataFrame({'Insight': c_1}, index=[0])
3
  • .append() to your df? Commented Aug 3, 2020 at 17:26
  • I would suggest creating a function and the use apply method in order to create the desired column. Commented Aug 3, 2020 at 17:56
  • This doesn't serve my objective. I have multiple dataframes, to begin with, and i have the logic to generate the comments from each of those in place. I cannot modify the original dataframe and need to create a completely new dataframe with values only from the generated for loop output. Few outputs needs to be clubbed as shown in the expected otuput2 and few to be updated rowwise as showon in the expected output 1. Is there anyway to save the variable to new df where variable is derived from the output of for statement? At the end i will be using append to join the new dataframes created Commented Aug 4, 2020 at 1:38

1 Answer 1

1

To flesh out @Cedroc Zoppolo's comment, here a minimal example with f strings.

    import pandas as pd

    df = pd.DataFrame({'Sales': [10, 20], 'Income': [20, -30]})
    
    def description(sales, income):
        return f'Sales up {sales}% while Total income {"up" if income > 0 else "down"} {income}%'

    df['descr'] = df.apply(lambda x: description(x['Sales'], x['Income']), axis=1)

Output:

   Sales  Income                                      descr
0     10      20     Sales up 10% while Total income up 20%
1     20     -30  Sales up 20% while Total income down -30%
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.