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
Typeerror: Unhashable Type: 'Dict'
How to Construct a Timedelta Object from a Simple String
How to Get the Input from the Tkinter Text Widget
How to Remove Non-Ascii Characters But Leave Periods and Spaces
Python: Access Class Property from String
Send File Using Post from a Python Script
Replace All Elements of Python Numpy Array That Are Greater Than Some Value
How to Dereference Variable Id'S
Pandas Add Column to Groupby Dataframe
How to Make Ball Bounce Off Wall with Pygame
Automatically Create Requirements.Txt
Compare Two Dataframes and Output Their Differences Side-By-Side
Is Python Interpreted, or Compiled, or Both
Differences Between Staticfiles_Dir, Static_Root and Media_Root
Is Shared Readonly Data Copied to Different Processes for Multiprocessing