简体   繁体   中英

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. 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.

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.

Or you can drop a column:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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