简体   繁体   English

在csv读取后,熊猫掉落第一列

[英]Pandas drop first columns after csv read

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. 我理解自我应该放在对象上下文中,但它在这里描述了我打算做的事情。

(Of course, doing this operation in two separate lines works perfectly.) (当然,在两个单独的行中执行此操作非常有效。)

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. 这里3是总列数,我想删除第2列。 You can directly write index of columns to use 您可以直接编写要使用的列索引

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM