2

Is there a simple way to ignore all even/odd rows when reading a csv using pandas?

I know skiprows argument in pd.read_csv but for that I'll need to know the number of rows in advance.

isherwood
  • 52,576
  • 15
  • 105
  • 143
nivniv
  • 3,111
  • 5
  • 28
  • 35

2 Answers2

3

The pd.read_csv skiprows argument accepts a callable, so you could use a lambda function. E.g.:

df = pd.read_csv(some_path, skiprows=lambda x: x%2 == 0)
Chris
  • 1,433
  • 12
  • 21
1

A possible solution after reading would be:

import pandas as pd
df = pd.read_csv(some_path)
# remove odd rows:
df = df.iloc[::2]
# remove even rows:
df = df.iloc[1::2]
nivniv
  • 3,111
  • 5
  • 28
  • 35