How to add header row to a pandas DataFrame

We Are Going To Discuss About How to add header row to a pandas DataFrame. So lets Start this Python Article.

How to add header row to a pandas DataFrame

How to solve How to add header row to a pandas DataFrame

You can use names directly in the read_csv
names : array-like, default None List of column names to use. If file
contains no header row, then you should explicitly pass header=None
Cov = pd.read_csv("path/to/file.txt", sep='\t', names=["Sequence", "Start", "End", "Coverage"])

How to add header row to a pandas DataFrame

You can use names directly in the read_csv
names : array-like, default None List of column names to use. If file
contains no header row, then you should explicitly pass header=None
Cov = pd.read_csv("path/to/file.txt", sep='\t', names=["Sequence", "Start", "End", "Coverage"])

Solution 1

You can use names directly in the read_csv

names : array-like, default None List of column names to use. If file
contains no header row, then you should explicitly pass header=None

Cov = pd.read_csv("path/to/file.txt", 
                  sep='\t', 
                  names=["Sequence", "Start", "End", "Coverage"])

Original Author Leb Of This Content

Solution 2

Alternatively you could read you csv with header=None and then add it with df.columns:

Cov = pd.read_csv("path/to/file.txt", sep='\t', header=None)
Cov.columns = ["Sequence", "Start", "End", "Coverage"]

Original Author Anton Protopopov Of This Content

Solution 3

col_Names=["Sequence", "Start", "End", "Coverage"]
my_CSV_File= pd.read_csv("yourCSVFile.csv",names=col_Names)

having done this, just check it with:

my_CSV_File.head()

Original Author Bhardwaj Joshi Of This Content

Solution 4

To fix your code you can simply change [Cov] to Cov.values, the first parameter of pd.DataFrame will become a multi-dimensional numpy array:

Cov = pd.read_csv("path/to/file.txt", sep='\t')
Frame=pd.DataFrame(Cov.values, columns = ["Sequence", "Start", "End", "Coverage"])
Frame.to_csv("path/to/file.txt", sep='\t')

But the smartest solution still is use pd.read_excel with header=None and names=columns_list.

Original Author romulomadu Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment