Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have written a for loop for the dataframe. I need to store the printed output into a variable to be used. how to store the printed variable. The variable which contains the "Non-Sachet" and "Sachet" Printed Values must be added to the dataframe as columns. As such if i know how to store the printed "Non-Sachet" and "Sachet" Values as list i can easily add the values in the variable as columns in dataframe

What I have tried:

Python
import pandas as pd
import numpy as np
df=pd.read_excel(r'D:\Practice Files\Anirudh Exercise Skin India.xlsx')
#Sachet/Non-Sachet
Sachet=df["Basepacksize"].tolist()
first_element=10
for int in Sachet:
   if first_element !=int:
    print("Non-Sachet")
   else:
      print("Sachet")
Posted
Updated 12-Dec-22 7:11am
v2
Comments
Richard MacCutchan 12-Dec-22 12:01pm    
As with your other questions, the problem is with your data. But unless you show it to us we cannot guess what is wrong with it. And the above code does not make a lot of sense.
Anirudh Sudarsan 12-Dec-22 12:21pm    
sry bro its company client data. NDA and all that sh*t and yes its the client who provide the data in excel.
The Main Purpose is to store all the Sachet and Non-Sachet Outputs and add it into the dataframe for analysis purpose. Thats why i asked how to store all the "Non-Sachet" and "Sachet" Printed Output in a variable so that it can be added to dataframe as a column.
Also i am a fresher currently on training. So any help is appreciated
Richard MacCutchan 12-Dec-22 12:32pm    
What do "Non-Sachet" and "Sachet" mean, they are not terms that I am familiar with? Also if you are on training then you should speak to your senior(s) about what is necessary. I assume someone within your company knows what this task is about.
Anirudh Sudarsan 12-Dec-22 12:38pm    
sachet means small things like 18 Cents Shampoo Packet. Also i would like to consult the net before going to them as a final resort. also is it possible to use things like closure() for storing the printed values. if so how to do it
Richard MacCutchan 12-Dec-22 12:43pm    
Well if you cannot show us the data you are working with, and explain exactly what you are trying to achieve then it is difficult for us to offer suggestions.

You need to sit down and work out exactly what you are doing, or trying to do: that code doesn't do anything particularly useful, and appears to have been "thrown together" with little or no planning.

Work out what you want to achieve, and start planning from there: what do you want to "store", how long do you want to store it, what are you going to do with it later? These are important, as a "store it so I can use it next time the app runs" solution is very different from a "store it so I can use it after the loop" would be.

And please, don't use Excel to store data: That's not a database, dude![^]
 
Share this answer
 
Why is Googling for this so hard to do?

how to store data in Excel using pandas - Google Search[^]
 
Share this answer
 
Comments
Anirudh Sudarsan 12-Dec-22 13:14pm    
Not Exactly I need to store the "Sachet" and "Non-Sachet" Printed Values in a python variable on analysis purpose. not storing a column on excel using pandas
Dave Kreskowiak 12-Dec-22 13:16pm    
OK, even worse. You're telling me you don't know how to store values in an array.

Python how to store values in an array - Google Search[^]
Anirudh Sudarsan 12-Dec-22 13:18pm    
did you fully read the question. i told that i need to store the "non-sachet" and "sachet" values printed by print command using if condition and for loop
Dave Kreskowiak 12-Dec-22 13:31pm    
Yeah, AND? I did read your entire question, which, frankly, doesn't make a whole lot of sense.

You can both print something on screen and store a string in an array. You seem to be fixed on redirecting print output to a variable and that's not really the way you should be doing that.
Anirudh Sudarsan 13-Dec-22 1:39am    
understood and i created a list and appended the output value to it. so consider this problem solved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900