Pandas read_csv skip rows condition

pandas read_csv skip rows condition

The read_csv() function in pandas library of Python is used to read the data from a CSV (Comma Separated Values) file into a DataFrame. It provides several options, including the ability to skip rows during the reading process.

When reading a CSV file, we can specify the number of rows to skip at the beginning of the file using the skiprows parameter. This parameter allows us to pass either an integer or a list-like of row indices to be skipped.

Examples:

  1. Skipping a single row:

    import pandas as pd
    
    # Read CSV file and skip the first row
    df = pd.read_csv('data.csv', skiprows=1)
    print(df)
          

    In this example, the ‘data.csv‘ file is read, and the first row is skipped during the reading process. The resulting DataFrame will not include the skipped row. Only the rows from the second row onwards will be present in the DataFrame.

  2. Skipping multiple rows:

    import pandas as pd
    
    # Read CSV file and skip the first two rows
    df = pd.read_csv('data.csv', skiprows=[0, 1])
    print(df)
          

    In this example, the ‘data.csv‘ file is read, and both the first and second rows are skipped. The resulting DataFrame will not include these skipped rows.

  3. Skipping rows based on condition:

    import pandas as pd
    
    # Read CSV file and skip rows where a certain column has a specific value
    df = pd.read_csv('data.csv', skiprows=lambda x: x % 2 == 0)
    print(df)
          

    In this example, the ‘data.csv‘ file is read, but rows are skipped based on a lambda function provided to skiprows. The lambda function checks if the index of the row is even (divisible by 2) and skips it if the condition is True. As a result, only the rows with odd indices will be present in the DataFrame.

The skiprows parameter of read_csv() provides flexibility in skipping rows while reading a CSV file into a DataFrame. It allows skipping a single row, multiple rows, or rows based on a particular condition.

Leave a comment