使用熊猫,我如何在没有标题的.csv文件中只读取列的子集(比如第4列和第7列)?我似乎不能用usecols来做这些。
当前回答
As per documentation https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html : headerint, list of int, default ‘infer’ Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of integers that specify row locations for a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file.
namesarray-like,可选 要使用的列名列表。如果文件包含标题行,则应该显式传递header=0以覆盖列名。此列表中的副本是不允许的。
columts = ['Day', 'PLMN', 'RNCname']
tempo = pd.read_csv("info.csv", sep=';', header=0, names=columts, index_col=False)
其他回答
以前的答案是正确的,但在我看来,一个额外的names参数将使它完美,这应该是推荐的方式,特别是当csv没有头文件时。
解决方案
使用usecols和names参数
df = pd.read_csv(file_path, usecols=[3,6], names=['colA', 'colB'])
更多的阅读
或者使用header=None显式地告诉人们csv没有header(无论如何这两行是相同的)
df = pd.read_csv(file_path, usecols=[3,6], names=['colA', 'colB'], header=None)
这样你就可以通过
# with `names` parameter
df['colA']
df['colB']
而不是
# without `names` parameter
df[0]
df[1]
解释
基于read_csv,当显式传递名称时,header将表现为None而不是0,因此当存在名称时可以跳过header=None。
为了读取一个没有标题的csv文件,并且只针对某些列,你需要为第4列和第7列传递params header=None和usecols=[3,6]:
df = pd.read_csv(file_path, header=None, usecols=[3,6])
查看文档
你也可以用header=None调用read_table()(读取文件的第一行作为数据的第一行):
df = pd.read_table('test.tsv', sep=',', usecols=[3,6], header=None)
如果分隔符是\t(,则此函数更有用。TSV文件等),因为默认分隔符是\t(不像read_csv的默认分隔符是,)。
确保指定pass header=None,并为第4列和第7列添加usecols=[3,6]。
As per documentation https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html : headerint, list of int, default ‘infer’ Row number(s) to use as the column names, and the start of the data. Default behavior is to infer the column names: if no names are passed the behavior is identical to header=0 and column names are inferred from the first line of the file, if column names are passed explicitly then the behavior is identical to header=None. Explicitly pass header=0 to be able to replace existing names. The header can be a list of integers that specify row locations for a multi-index on the columns e.g. [0,1,3]. Intervening rows that are not specified will be skipped (e.g. 2 in this example is skipped). Note that this parameter ignores commented lines and empty lines if skip_blank_lines=True, so header=0 denotes the first line of data rather than the first line of the file.
namesarray-like,可选 要使用的列名列表。如果文件包含标题行,则应该显式传递header=0以覆盖列名。此列表中的副本是不允许的。
columts = ['Day', 'PLMN', 'RNCname']
tempo = pd.read_csv("info.csv", sep=';', header=0, names=columts, index_col=False)
推荐文章
- model.eval()在pytorch中做什么?
- Tensorflow 2.0:模块“Tensorflow”没有属性“Session”
- 从环境文件中读入环境变量
- 在OSX 10.11中安装Scrapy时,“OSError: [Errno 1]操作不允许”(El Capitan)(系统完整性保护)
- 如何删除熊猫数据帧的最后一行数据
- 我如何在熊猫中找到数字列?
- 检查pandas数据框架索引中是否存在值
- 计算熊猫数量的最有效方法是什么?
- 如何在python中验证日期字符串格式?
- 用csv模块从csv文件中读取特定的列?
- 使用PyCrypto AES-256加密和解密
- “\d”在正则表达式中是数字吗?
- Python中的否定
- 列表推导式中的Lambda函数
- 如何删除文件中的特定行?