Python 如何将标题行添加到 Pandas DataFrame

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34091877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:27:23  来源:igfitidea点击:

How to add header row to a pandas DataFrame

pythoncsvpandasheader

提问by sequence_hard

I am reading a csv file into pandas. This csv file constists of four columns and some rows, but does not have a header row, which I want to add. I have been trying the following:

我正在将一个 csv 文件读入pandas. 这个 csv 文件由四列和一些行组成,但没有我想添加的标题行。我一直在尝试以下方法:

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

But when I apply the code, I get the following Error:

但是当我应用代码时,出现以下错误:

ValueError: Shape of passed values is (1, 1), indices imply (4, 1)

What exactly does the error mean? And what would be a clean way in python to add a header row to my csv file/pandas df?

错误究竟是什么意思?在 python 中向我的 csv 文件/pandas df 添加标题行的干净方法是什么?

采纳答案by Leb

You can use namesdirectly in the read_csv

您可以names直接在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

名称:类似数组,默认无要使用的列名列表。如果文件不包含标题行,则应明确传递 header=None

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

回答by Anton Protopopov

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

或者,您可以使用以下内容读取 csv,header=None然后添加df.columns

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

回答by Bhardwaj Joshi

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

having done this, just check it with[well obviously I know, u know that. But still...

完成此操作后,只需使用[很明显我知道,你知道。但是还是...

my_CSV_File.head()

Hope it helps ... Cheers

希望它有帮助......干杯

回答by romulomadu

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

要修复您的代码,您可以简单地更改[Cov]Cov.values, 的第一个参数pd.DataFrame将成为一个多维numpy数组:

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_excelwith header=Noneand names=columns_list.

但最聪明的解决方案仍然是pd.read_excelheader=None和 一起使用names=columns_list