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?
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']]
df[‘math_grade’] = df[‘grades’].apply(lambda x : x[2])
will do the job
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With