curious
curious

Reputation: 17

skip_blank_lines is not working with pandas

test_scores = pd.DataFrame({'id' : [1, 2, '', 4, 5], 
'first_name' : ['Sachin', 'Dravid', '', 'Virat', 'Yuvraj'],
'scores' : [150, 210, '', 125, 75],
'state' : ['Mumbai', 'Karnataka','', 'Delhi', 'Punjab']})

test_scores.to_csv("test_scores.csv", sep = ',', header = True, index = False)

skip = pd.read_csv(filepath_or_buffer = 'test_scores.csv', sep = ',', header = 0, skip_blank_lines = True)

skip

I wanted to remove the blank lines the row with NaN values. But even after applying skip_blank_lines = True, the NaN values are not skipped or removed.

Upvotes: 1

Views: 363

Answers (1)

mozway
mozway

Reputation: 261015

Your line is not blank, it has all empty values. You need to use dropna:

skip = (pd.read_csv(filepath_or_buffer='test_scores.csv', sep=',', header=0,
                    skip_blank_lines=True, # not needed here
                    )
          .dropna(how='all')
       )

output:

    id first_name  scores      state
0  1.0     Sachin   150.0     Mumbai
1  2.0     Dravid   210.0  Karnataka
3  4.0      Virat   125.0      Delhi
4  5.0     Yuvraj    75.0     Punjab

Upvotes: 1

Related Questions