Pandas 在 csv 读取后删除第一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49572579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas drop first columns after csv read
提问by Ti me
Is there a way to reference an object within the line of the instantiation ?
有没有办法在实例化行中引用对象?
See the following example : I wanted to drop the first column (by index) of a csv file just after reading it (usually pd.to_csv outputs the index as first col) :
请参见以下示例:我想在读取后删除 csv 文件的第一列(按索引)(通常 pd.to_csv 将索引输出为第一列):
df = pd.read_csv(csvfile).drop(self.columns[[0]], axis=1)
I understand self should be placed in the object context but it here describes what I intent to do.
我理解 self 应该放在对象上下文中,但它在这里描述了我打算做什么。
(Of course, doing this operation in two separate lines works perfectly.)
(当然,在两个单独的行中执行此操作非常有效。)
采纳答案by jpp
One way is to use pd.DataFrame.iloc
:
一种方法是使用pd.DataFrame.iloc
:
import pandas as pd
from io import StringIO
mystr = StringIO("""col1,col2,col3
a,b,c
d,e,f
g,h,i
""")
df = pd.read_csv(mystr).iloc[:, 1:]
# col2 col3
# 0 b c
# 1 e f
# 2 h i
回答by Aritesh
Assuming you know the total number of columns in the dataset, and the indexes you want to remove -
假设您知道数据集中的总列数,以及要删除的索引 -
a = range(3)
a.remove(1)
df = pd.read_csv('test.csv', usecols = a)
Here 3 is the total number of columns, and I wanted to remove 2nd column. You can directly write index of columns to use
这里 3 是总列数,我想删除第二列。您可以直接编写要使用的列索引