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])
.append()to your df?applymethod in order to create the desired column.