Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting the nth element from each list and storing it in a new column [duplicate]

I've a dataframe (called 'df') which contains a column called 'grades'. This column contains a list of grades. The data in this column is of type 'object'.

    student_id    grades
0       11      [A,A,B,A]
1       12      [B,B,B,C]
2       13      [C,C,D,B]
3       21      [B,A,C,B] 

I'm hoping to create a new column called 'maths_grades', which will store the 3rd element in the grades list.

Example Output:

      student_id   grades    maths_grade
0       11      [A,A,B,A]        B
1       12      [B,B,B,C]        B
2       13      [C,C,D,B]        D
3       21      [B,A,C,B]        C  

Whats best was to go about this?

like image 333
Mark Kennedy Avatar asked Oct 13 '25 05:10

Mark Kennedy


2 Answers

Use indexing with str, because working with iterables:

df['maths_grade'] = df['grades'].str[2]

Or list comprehension if no missing values and performance is important:

df['maths_grade'] = [x[2] for x in df['grades']]
like image 174
jezrael Avatar answered Oct 14 '25 18:10

jezrael


df[‘math_grade’] = df[‘grades’].apply(lambda x : x[2]) will do the job

like image 22
Chris Avatar answered Oct 14 '25 19:10

Chris