如何将标题行添加到 pandas DataFrame

How to add header row to a pandas DataFrame(如何将标题行添加到 pandas DataFrame)
本文介绍了如何将标题行添加到 pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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/to/file.txt", sep='	')
Frame=pd.DataFrame([Cov], columns = ["Sequence", "Start", "End", "Coverage"])
Frame.to_csv("path/to/file.txt", sep='	')

但是当我应用代码时,我得到以下错误:

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

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

这个错误到底是什么意思?在 python 中将标题行添加到我的 csv 文件/pandas df 的一种干净的方法是什么?

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?

推荐答案

你可以直接在names稳定/生成/pandas.read_csv.html" rel="noreferrer">read_csv

You can use names directly in the read_csv

names : 类似数组,默认 None 要使用的列名列表.如果文件不包含标题行,那么您应该显式传递 header=None

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='	', 
                  names=["Sequence", "Start", "End", "Coverage"])

这篇关于如何将标题行添加到 pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)