Skip Rows During CSV Import Pandas

Skip rows during csv import pandas

You can try yourself:

>>> import pandas as pd
>>> from StringIO import StringIO
>>> s = """1, 2
... 3, 4
... 5, 6"""
>>> pd.read_csv(StringIO(s), skiprows=[1], header=None)
0 1
0 1 2
1 5 6
>>> pd.read_csv(StringIO(s), skiprows=1, header=None)
0 1
0 3 4
1 5 6

Pandas read_csv skiprows with conditional statements

No. skiprows will not allow you to drop based on the row content/value.

Based on Pandas Documentation:

skiprows : list-like, int or callable, optional

Line numbers to skip (0-indexed) or
number of lines to skip (int) at the start of the file.
If callable, the callable function will be evaluated against the row indices, returning True if the row should be skipped and False
otherwise. An example of a valid callable argument would be lambda x:
x in [0, 2]
.

pandas read_csv() skiprows=[0] giving issues?

I think parameter skiprows here is not necessary, you can omit it.

But if pass 0 values it means don't skip any rows:

skiprows=0

import pandas as pd
from io import StringIO

temp="""Site,Tank ID,Product,Volume,Temperature,Dip Time
aaa,bbb,ccc,ddd,eee,fff
a,b,c,d,e,f
"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp))
print (df)
Site Tank ID Product Volume Temperature Dip Time
0 aaa bbb ccc ddd eee fff
1 a b c d e f

temp="""Site,Tank ID,Product,Volume,Temperature,Dip Time
aaa,bbb,ccc,ddd,eee,fff
a,b,c,d,e,f
"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), skiprows=0)
print (df)
Site Tank ID Product Volume Temperature Dip Time
0 aaa bbb ccc ddd eee fff
1 a b c d e f

But if pass [0] it means remove first row of file, here header, it means "skip the 0'th row, i.e. the headed row:

temp="""Site,Tank ID,Product,Volume,Temperature,Dip Time
aaa,bbb,ccc,ddd,eee,fff
a,b,c,d,e,f
"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), skiprows=[0])
print (df)
aaa bbb ccc ddd eee fff
0 a b c d e f


Related Topics



Leave a reply



Submit