读入数据帧(不包括第一列)

2024-04-26 05:59:41 发布

您现在位置:Python中文网/ 问答频道 /正文

在将文本文件读取到数据帧时,我应该如何排除第一列并读取它

当前使用的代码:

dframe_main =pd.read_table('/Users/ankit/Desktop/input.txt',sep =',')

Tags: 数据代码txtreadinputmaintableusers
2条回答

你看完后把这列删掉就够了吗?这在功能上与从读取中排除第一列相同。以下是一个玩具示例:

import numpy as np
import pandas as pd
data = np.array([[1,2,3,4,5], [2,2,2,2,2], [3,3,3,3,3], [4,4,3,4,4], [7,2,3,4,5]])
columns = ["one", "two", "three", "four", "five"]
dframe_main = pd.DataFrame(data=data, columns=columns)
print "All columns:"
print dframe_main
del dframe_main[dframe_main.columns[0]] # get rid of the first column
print "All columns except the first:"
print dframe_main

输出为:

All columns:
   one  two  three  four  five
0    1    2      3     4     5
1    2    2      2     2     2
2    3    3      3     3     3
3    4    4      3     4     4
4    5    2      3     4     5

All columns except the first:
   two  three  four  five
0    2      3     4     5
1    2      2     2     2
2    3      3     3     3
3    4      3     4     4
4    2      3     4     5

我建议使用usecols参数:

usecols : array-like, default None Return a subset of the columns.

Results in much faster parsing time and lower memory usage.

假设文件有5列:

In [32]: list(range(5))[1:]
Out[32]: [1, 2, 3, 4]

dframe_main = pd.read_table('/Users/ankit/Desktop/input.txt', usecols=list(range(5))[1:])

相关问题 更多 >