如何从没有coulmn头的文本文件中提取一个特定列到panda数据帧中

2024-04-23 17:56:40 发布

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

我创建了一个csv文件-数据aa.csv,输入列标题“operation”以指定要提取的列,并使用以下代码。在

data = pd.read_csv('dataaa.csv')
df1=data.loc[:,"operation"]

它在工作。但现在我想把它扩展到一个真实的情况下

我需要在5210文件上迭代相同的过程,这是中split命令的结果linux输出文件以文件名xxa开头。但是它没有包含一个列标题“operation”,如何读取文件中第二列的列,它足够遍历大量的文件。在


Tags: 文件csv数据代码标题readdata过程
1条回答
网友
1楼 · 发布于 2024-04-23 17:56:40

您可以使用read_cv函数中的usecols关键字。查看完整的documentation。在

data = pd.read_csv('dataaa.csv', usecols=[1], header=None)

usecols : array-like or callable, default None

Return a subset of the columns. If array-like, all elements must either be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in names or inferred from the document header row(s). For example, a valid array-like usecols parameter would be [0, 1, 2] or [‘foo’, ‘bar’, ‘baz’].

If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. An example of a valid callable argument would be lambda x: x.upper() in ['AAA', 'BBB', 'DDD']. Using this parameter results in much faster parsing time and lower memory usage.

相关问题 更多 >