How to import every nth row from a CSV file in Python?

I want to accomplish a very specific task but I don’t have an idea of how to do it in Python. The task involves importing every nth row from a CSV file, for example, I want to import every 10th row present in my CSV file. Please provide me with methods and techniques that are available that can solve this problem, also provide some code snippets as it makes it easy to understand. You can use the Boston dataset for your code using the link attached below:

file_link = 'https://tinyurl.com/BostonHousingcsv'

Hey @mubashir_rizvi, NumPy and the slicing method can be used to solve this. Here is the code for it:

  • The np.genfromtxt() function is used to load data from a text file, including CSV files, into a NumPy array.
  • [1: : n] is used to skip the 0th item from the array (which is the column names) and the 3rd argument in the slicer is used as a step interval to get every nth item which in this case is 10.
  • Since we skipped the column names, we specify them when we create a DataFrame using the pd.DataFrame() constructor.

Hello @mubashir_rizvi, you can do this by using the skiprows argument of read_csv(). The skiprows argument of the read_csv() function is used to skip and ignore rows while reading a CSV file. Let me share an example code with you.