0

(I'm pretty new to Python,(and even to coding)forgive me for my stupidity.) I'm trying to pass a text value and a list as parameters to a function. Here's an example :

Names = File['Student_Name']
Scores = File['Marks']
for a in range(0,100):
    Student_Name = [Names[a]]
    Marks = []
    NewDf = pd.DataFrame(PreCovid(Student_Name,Marks))
Master_Sheet_PreCovid = NewDf
Master_Sheet_PreCovid

What I wish to achieve is passing Name of a Student, as a string, one at a time, to the function. In this code, I'm vaguely creating a df with each loop iteration, which obviously will only return me the last value, however, I wish to get the output for complete list of Students. What modifications/additions do I make in this code to make it work. I followed this thread, Why the function is only returning the last value? , which was similar to my query, however might not work with my requirements.

Edited : I actually have 2 sheets that I'm fetching my data from,one is a Main Sheet,that has all the data with redundancy,I've a Rule book with unique values and the rules for calculation.In this code I'm only fetching values from Rule Book,then going to the function,fetching data based on these values from Main Sheet,performing my calculations,creating a new dataframe,inserting the values I get here into that dataframe as well,and return the Final dataframe.Right now, the calculation tested based only on Student_Name has worked, but now I've a bigger problem of calculating also based on Marks.

At the risk of sounding arrogant, I only wish to pass the name as string, not as list. Again, I'm sorry about the stupidity of my query.

mamta_rao
  • 29
  • 4
  • 1
    If you want to append each row to an existing dataframe, one at a time, you should create a dictionary with column names as keys and corresponding cell values for that row as the values. See this: [append-dictionary-to-data-frame](https://stackoverflow.com/questions/51774826/append-dictionary-to-data-frame) and the [Documentation for `pandas.DataFrame.append`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html). – CypherX Nov 11 '20 at 07:41
  • What is `PreCovid()`? Is it a function? Are you extracting information from another dataframe for PreCovid? In most of the cases you can altogether drop the `loop`, as vectorized methods in pandas are much faster. – CypherX Nov 11 '20 at 07:52
  • Yes, PreCovid is a function that's taking Student_Name(String) and marks(List)as parameters – mamta_rao Nov 11 '20 at 08:16
  • what does the function return? – CypherX Nov 11 '20 at 08:18
  • It returns a dataframe, which has columns calculated based on the arguments passed from the loop. – mamta_rao Nov 11 '20 at 08:46
  • I’m not sure if it is a dataframe. Your code in the question says otherwise. Why would you make a dataframe by passing in another dataframe and effectively making no changes? – CypherX Nov 11 '20 at 17:45
  • I've edited my question for a better understanding. – mamta_rao Nov 12 '20 at 03:55

1 Answers1

0

Give it a try:

Names = File['Student_Name']
Scores = File['Marks']
Master_Sheet_PreCovid = []
for a in range(0,100):
    Student_Name = [Names[a]]
    Marks = []
    NewDf = pd.DataFrame(PreCovid(Student_Name,Marks))
    Master_Sheet_PreCovid.append(NewDf)
Master_Sheet_PreCovid = pd.concat(Master_Sheet_PreCovid)
print(Master_Sheet_PreCovid)
mccandar
  • 778
  • 8
  • 16
  • Thanks a ton! This has worked. I however have another requirement now, I've edited my question for better understanding. – mamta_rao Nov 12 '20 at 04:21