python新手代码图案如何保存
python添加标题⾏的代码_如何添加标题⾏到
pandasDataFrame
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:
Cov = pd.read_csv("path/", sep='\t')
Frame=pd.DataFrame([Cov], columns = ["Sequence", "Start", "End", "Coverage"])
<_csv("path/", 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?
解决⽅案
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/", sep='\t',
names = ["Sequence", "Start", "End", "Coverage"])
The line below will not work as you expect. Cov is already a dataframe, assuming it really has 4 columns when it's being read from the file.
Frame=pd.DataFrame([Cov], columns = ["Sequence", "Start", "End", "Coverage"])