我正在将一个 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')
但是当我应用代码时,我得到以下错误:
ValueError: Shape of passed values is (1, 1), indices imply (4, 1)
这个错误到底是什么意思?在 python 中将标题行添加到我的 csv 文件/pandas df 的一种干净的方法是什么?
最佳答案
您可以在 read_csv
中直接使用 names
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"])
关于python - 如何将标题行添加到 Pandas DataFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34091877/