Viral Parmar
Viral Parmar

Reputation: 488

Getting rows of data frame excluding header in pandas

I have a CSV file of which the screenshot is shown below : enter image description here My goal is to get the rows of the data frame excluding the header part.

My Purpose: I'm converting data in row into "float64" using the function astype() and then rounding the data to two decimal point using pandas round() function. The code for the same is shown below:

df = pd.read_csv('C:/Users/viral/PycharmProjects/Samples/036_20191009T132130.CSV',skiprows = 1)
df = df.astype('float64', errors="ignore")
df = df.round(decimals=2)

Here as you can see that I'm Skipping the first row in order to exclude the header part.

But Unfortunately, the data is not rounding up to 2 decimals place. The results are shown below : enter image description here

I am not sure, but I guess the line Empty Data frame is making a problem in rounding up the data.

With Header= None enter image description here

Even header= 1 will act in the same way the skiprow=1 was acting.

Any suggestions are most welcome...

Thank you

Upvotes: 1

Views: 3371

Answers (2)

jezrael
jezrael

Reputation: 863031

I believe you need:

file = 'C:/Users/viral/PycharmProjects/Samples/036_20191009T132130.CSV'

#for default columns 0,1,2, N with omit first row in original data
df = pd.read_csv(file,skiprows = 1, header=None)
#for columns names by first row in file omit skiprows and header parameters
#df = pd.read_csv(file)
#if necessary, convert to floats
df = df.astype('float64', errors="ignore")
#select only numeric columns
cols = df.select_dtypes(np.number).columns
#round only numeric cols
df[cols] = df[cols].round(decimals=2)

Upvotes: 3

artemis
artemis

Reputation: 7281

Try the following:

df = pd.read_csv('C:/Users/viral/PycharmProjects/Samples/036_20191009T132130.CSV',skiprows = 1)
df = df.astype('float64', errors="ignore")
df = df.round(decimals=2)

Upvotes: 0

Related Questions