Skipping certain rows

We can also skip certain rows. Let's say that we only want the rows whose indices are multiples of 3: 

from io import StringIO
data = 'col1,col2,col3 a,b,1 a,b,2 c,d,3 c,e,4 g,f,5 e,z,6'
pd.read_csv(StringIO(data),skiprows=lambda x: x % 3 != 0)

We get the following output:

Demonstration of using the skiprows parameter in read_csv. The right-hand panel shows the data that's been filtered through skiprows (keeping only rows with row numbers that are multiples of 3)

The left-hand side diagram shows the resultant DataFrame without skipping any row, while the right-hand side shows the same DataFrame after filtering the rows whose indices are not multiples of 3. Note that this method considers the real index (3rd and 6th from the top, starting from 1) and not the Python index (starting from 0) for filtering the rows based on their index.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset