0

I have the following df in python:

Course  |Student 1 | Student 2 | Student 3
--------|----------|-----------|---------
Course2 | 1.1      | empty     | empty
Course2 | empty    | 5.3       | empty
Course2 | empty    | empty     | 4.2

However, I want to have the following df:

Course  |Student 1 | Student 2 | Student 3
--------|----------|-----------|---------
Course2 | 1.1      | 5.3       | 4.2

How can I do this?

wjandrea
  • 23,210
  • 7
  • 49
  • 68
Tobias
  • 33
  • 5

3 Answers3

3

As numbers evaluate before letters, you can groupby "Course" and take the min:

df.groupby('Course').agg('min')
mozway
  • 81,317
  • 8
  • 19
  • 49
1

If you have different datatypes(or in your current scenario also) in your real data then you can use first():

# df = df.replace('empty', float('NaN'))
df = df.groupby('Course', as_index=False).first()

output:

    Course Student 1 Student 2 Student 3
0  Course2       1.1       5.3       4.2
wjandrea
  • 23,210
  • 7
  • 49
  • 68
Anurag Dabas
  • 23,002
  • 8
  • 19
  • 34
0

I fixed it with the following code:

df.replace('empty', None)
Tobias
  • 33
  • 5