Abhishek Gaur
Abhishek Gaur

Reputation: 300

Remove header Row in Python 3.4.2 using Pandas

I have a csv I want to remove the Header row. So that second row becomes Header

I have a csv I want to remove the Header row. So that second row becomes Header I have rows which I wanted to delete in the starting. But it takes one column value and makes it a header like:

      Time Deposits     Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4  \
4   Settlement Date  Maturity Date   Order ID     Status     Entity   

I want to delete the top Header Line so that Line below act as a Header and I can do further process.

If I do df7 = df6.iloc[1:] it removes

Settlement Date  Maturity Date   Order ID     Status     Entity

but I want to remove:

Time Deposits     Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4  \

import pandas as pd


# making data frame from csv file 
df1 = pd.read_csv("E:\Sample\LORD ABBETT REPO SHEET 1-14-19_Notes_2.csv") 

df2=df1.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False)

#df2.index = np.arange(1, len(df2) + 1)

#df3=df2.drop(['Time Deposits'])


print (df2)

Now:

      Time Deposits     Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4  \
4   Settlement Date  Maturity Date   Order ID     Status     Entity  

Expected:

4   Settlement Date  Maturity Date   Order ID     Status     Entity   

Upvotes: 2

Views: 11667

Answers (1)

lexual
lexual

Reputation: 48682

I suggest you look at the header argument that you can provide to pd.read_csv.

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html

I think specifically for you example, if you read the csv in like this:

df1 = pd.read_csv(
    "E:\Sample\LORD ABBETT REPO SHEET 1-14-19_Notes_2.csv",
    header=1) 

Then I believe you will have the column names that you are after, & won't have to munge your dataframe to get the correct data you want.

Upvotes: 4

Related Questions