Search code examples
pythonkeyerror

Change the strings in student_grade_str into int


I wanna change the strings in student_grade_str into int.

Here is my code:

import pandas as pd 
from google.colab import drive

drive.mount('/content/drive')
student_grade_str =pd.read_csv('/content/drive/MyDrive/student_power_research.csv')      
for i in range( len( student_grade_str)):
  student_grade_int = list(map(int(),student_grade_str[i]))

And here's student_power_research.csvPicture version

I ran this code with google colab. Result is Keyerror: 0. I don't understand what that error means. Please tell me how to fix this.


Solution

  • from google.colab import drive
    drive.mount('/content/drive')
    student_grade_str = pd.read_csv('/content/drive/MyDrive/student_power_research.csv')                  
    student_grade_str = student_grade_str["Number"].astype(int)
    student_grade_str = student_grade_str["Language"].astype(int)
    student_grade_str = student_grade_str["Logic"].astype(int)
    

    No need of for loop to convert string to int. Change your code as above.