# IPython for demo:
import pandas as pd
df = pd.read_csv('ballet.txt', sep='\t', header=None, names=['artists', 'tracks'], usecols=[2, 3])
# usecols here limits the Dataframe to only consist the 3rd and 4th column of your .txt
您的数据帧可能看起来像:
df
# Out:
artists tracks
0 Spandau Ballet Through The Barricades
1 Berlin Ballet Swan Lake
按列名访问单列:
df.artists # or by their index e.g. df.iloc[:, 0]
# Out:
0 Spandau Ballet
1 Berlin Ballet
Name: 2, dtype: object
使用python且不使用第三方软件包的选项:
输出:
[-2::]
获取每行的最后2列,如果需要,调整以获取其他列。你知道吗最好使用
pandas
-模块将.txt
的内容加载到一个DataFrame
中,然后从那里开始。如果你不熟悉它…一个DataFrame
与使用Python可以获得的Excelsheet非常接近。pandas
将为您处理读取行的操作,这样您就不必编写自己的循环。你知道吗假设您的文本文件是四列的,以制表符分隔,则如下所示:
您的数据帧可能看起来像:
按列名访问单列:
你现在仍然可以把数据放到数组中,但是如果你知道其他的选择,我想不出你真正想这样做的原因。你知道吗
如果文件中的列由表格分隔,则可以使用
np.loadtxt
(NumPy函数)执行以下操作这将输出一个NumPy数组。或者,您可以将这些数组转换为常规的Python字符串列表
相关问题 更多 >
编程相关推荐