使用 pandas 阅读时,如何删除csv文件的特定列? [英] How to drop a specific column of csv file while reading it using pandas?

查看:143
本文介绍了使用 pandas 阅读时,如何删除csv文件的特定列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用pandas加载csv时,我需要删除标签为 name .我正在阅读csv,如下所示,并希望在其中添加参数.谢谢.

I need to remove a column with label name at the time of loading a csv using pandas. I am reading csv as follows and want to add parameters inside it to do so. Thanks.

pd.read_csv("sample.csv")

我在阅读csv后知道要这样做:

I know this to do after reading csv:

df.drop('name', axis=1)

推荐答案

如果您事先知道列名,则可以通过设置usecols参数

If you know the column names prior, you can do it by setting usecols parameter

当您知道要使用哪些列

假设您有带有['id','name','last_name']列的csv文件,而您只想['name','last_name'].您可以按照以下步骤进行操作:

Suppose you have csv file with columns ['id','name','last_name'] and you want just ['name','last_name']. You can do it as below:

import pandas as pd
df = pd.read_csv("sample.csv", usecols = ['name','last_name'])

何时需要前N列

如果您不知道列名,但是要从数据框中获取前N列.您可以通过

If you don't know the column names but you want first N columns from dataframe. You can do it by

import pandas as pd
df = pd.read_csv("sample.csv", usecols = [i for i in range(n)])

修改

当您知道要删除的列的名称

# Read column names from file
cols = list(pd.read_csv("sample_data.csv", nrows =1))
print(cols)

# Use list comprehension to remove the unwanted column in **usecol**
df= pd.read_csv("sample_data.csv", usecols =[i for i in cols if i != 'name'])

这篇关于使用 pandas 阅读时,如何删除csv文件的特定列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆