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
  • 28,235
  • 9
  • 60
  • 81
Tobias
  • 33
  • 7

3 Answers3

3

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

df.groupby('Course').agg('min')
mozway
  • 194,879
  • 13
  • 39
  • 75
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
  • 28,235
  • 9
  • 60
  • 81
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
0

I fixed it with the following code:

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