简体   繁体   English

在Python中使用Numpy / Pandas读取文本文件

[英]Reading a text file with Numpy/Pandas in Python

I have an unlabeled dataset which contains 101 columns ie no column have any heading, now I want to start reading from column 2 to end ie 101th column. 我有一个未标记的数据集,其中包含101列,即没有列具有任何标题,现在我要开始从第2列开始读取,直到第101列结束。 I am trying with this code : 我正在尝试使用此代码:

data = np.loadtxt('structure-safety.inp', usecols = [2, 101])
or 
data = pd.read_fwf('structure-safety.inp', usecols = [2,1,101])

I also tried with usecols = [2, :], [2:101] or used () instead of [] but in vain, can anyone help me here with this problem. 我也尝试过用usecols = [2,:],[2:101]或使用()代替[],但是徒劳的,有人可以帮助我解决这个问题。

Try: 尝试:

data = pd.read_fwf('structure-safety.inp', usecols=list(range(2,102))

Keep in mind that the columns are numbered from 0. So column 2 is actually the third column. 请记住,列从0开始编号。因此,列2实际上是第三列。

Or you can drop a column: 或者,您可以删除一列:

data = pd.read_fwf('structure-safety.inp')
data = data.drop(df.columns[0:2], axis=1)

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

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