0

I am using pandas for the first time, and in the data frame there is a piece of data that is a percentage as a string but when put into a dataframe and printed it shows up as NaN. So to test it i just used a print statement and it returns the value fine. Thank you!

def makeSheet():
  data = [schoolName, location, aceptanceRate, tuition, roomAndBoard, debtAtGrad, studentToTeacher, satReq, totalUndergrad]
  dataDictonary = {"School Name": schoolName, "Location": location, "Aceptance Rate": aceptanceRate, "Tuition": tuition, "Room & Board": roomAndBoard, "Estimated Debt at Grad": debtAtGrad, "Student:Teacher": studentToTeacher,
                   "SAT Req": satReq, "Total Number of Undergrads": totalUndergrad}
  df = pd.DataFrame(dataDictonary, columns=headers, index=[0])
  print(df)
  print(aceptanceRate)

the pandas data table returned: pandas data table

The print statement returned: 31%

Answer: I had to ensure the spelling was the same across both acceptance rates. As in the dataDictonary one had to match the headers one. Thank you to Ignatius Reilly for the help

Crowbin
  • 1
  • 2
  • 1
    It would help if you can post a [minimal reproducible example](https://stackoverflow.com/questions/20109391). – Ignatius Reilly Apr 26 '23 at 01:14
  • 2
    The picture you shown has a column called `Acceptance Rate`, however, the code you used to create the df has a column called `Aceptance Rate` (with one 'c'). – Ignatius Reilly Apr 26 '23 at 01:17
  • From [pandas documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html) about setting `columns` to a value not None: _If data contains column labels, will perform column selection_. So, it's selecting only the keys from `dataDictonary` that are called the same as the names from `headers`. – Ignatius Reilly Apr 26 '23 at 01:31
  • Ah, thank you all! It works I had to change the naming of the title to match the variable. – Crowbin Apr 26 '23 at 01:41

0 Answers0