【问题标题】:Importing file containing text and numerical data using Python使用 Python 导入包含文本和数字数据的文件
【发布时间】:2019-03-01 16:41:22
【问题描述】:

我有一个 .txt 文件,其中包含文本数据和数字数据。文件的前两行有文本数据形式的基本信息,而第一列(我将第零列称为第一列)也有文本形式的基本数据。在文件中的所有其他位置,数据都是数字形式。我希望使用 python 中的库来分析文件中存在的数值数据,最好是 numpy 或 pandas,或两者的组合(回归、相关性、scikit-learn 等分析)。我重申文件中的所有数据对于我的分析都是必不可少的。以下快照(取自 Excel)显示了我的数据所在格式的截断版本:

此快照中显示的数据可以找到here.

特别是,我想要的是能够使用python(numpy或pandas)从这个文件中导入所有的数值数据,并且能够使用前两行中的文本数据来引用这个数据中的特定行(类型、标签)和第一列(对象编号)。在我的实际数据文件中,我有数十万行(对象类型)和数十个列。

我已经尝试使用numpy.loadtxt(...)pandas.read_csv(...) 打开这个文件,但是我遇到了错误,或者加载了笨拙格式的数据。我将非常感谢您对如何以某种方式在 python 中导入文件有一些指导,以便拥有我想要的功能。

【问题讨论】:

    标签: python python-2.7 pandas numpy


    【解决方案1】:

    如果我是你,我会使用 pandas,然后使用类似这样的方式导入它:

    df = pd.read_csv('dum.txt',sep='\t', header=[0,1], index_col=0)
    

    这为您提供了数据框:

    >>> df
    Type      T1   T2   T3   T4   T5
    Tag     Good Good Good Good Good
    object1  1.1  2.1  3.1  4.1  5.1
    object2  1.2  2.2  3.2  4.2  5.2
    object3  1.3  2.3  3.3  4.3  5.3
    object4  1.4  2.4  3.4  4.4  5.4
    object5  1.5  2.5  3.5  4.5  5.5
    object6  1.6  2.6  3.6  4.6  5.6
    object7  1.7  2.7  3.7  4.7  5.7
    object8  1.8  2.8  3.8  4.8  5.8
    

    而且你所有的列都是浮动的:

    >>> df.dtypes
    Type  Tag 
    T1    Good    float64
    T2    Good    float64
    T3    Good    float64
    T4    Good    float64
    T5    Good    float64
    dtype: object
    

    它包含一个多索引的列标题:

    >>> df.columns
    MultiIndex(levels=[['T1', 'T2', 'T3', 'T4', 'T5'], ['Good']],
               labels=[[0, 1, 2, 3, 4], [0, 0, 0, 0, 0]],
               names=['Type', 'Tag'])
    

    还有一个包含来自Type的信息的常规索引:

    >>> df.index
    Index(['object1', 'object2', 'object3', 'object4', 'object5', 'object6',
           'object7', 'object8'],
          dtype='object')
    

    此外,您可以将值转换为 numpy 数组 floats,只需使用:

    >>> df.values
    array([[1.1, 2.1, 3.1, 4.1, 5.1],
           [1.2, 2.2, 3.2, 4.2, 5.2],
           [1.3, 2.3, 3.3, 4.3, 5.3],
           [1.4, 2.4, 3.4, 4.4, 5.4],
           [1.5, 2.5, 3.5, 4.5, 5.5],
           [1.6, 2.6, 3.6, 4.6, 5.6],
           [1.7, 2.7, 3.7, 4.7, 5.7],
           [1.8, 2.8, 3.8, 4.8, 5.8]])
    

    【讨论】:

    • Sacul:非常感谢,这真的很有帮助 =)
    • 很高兴能帮上忙!
    【解决方案2】:

    使用sep\s 用于任何空格,不仅是制表符,engine='python' 用于删除警告:

    df=pd.read_csv('dum.txt',engine='python',sep='\s')
    print(df)
    

    输出:

          Type    T1    T2    T3    T4    T5
    0      Tag  Good  Good  Good  Good  Good
    1  object1   1.1   2.1   3.1   4.1   5.1
    2  object2   1.2   2.2   3.2   4.2   5.2
    3  object3   1.3   2.3   3.3   4.3   5.3
    4  object4   1.4   2.4   3.4   4.4   5.4
    5  object5   1.5   2.5   3.5   4.5   5.5
    6  object6   1.6   2.6   3.6   4.6   5.6
    7  object7   1.7   2.7   3.7   4.7   5.7
    8  object8   1.8   2.8   3.8   4.8   5.8
    

    或者如果想要两行列(我不推荐,因为那样很难使用):

    df=pd.read_csv('dum.txt',engine='python',sep='\s',header=[0,1])
    print(df)
    

    输出:

          Type   T1   T2   T3   T4   T5
           Tag Good Good Good Good Good
    0  object1  1.1  2.1  3.1  4.1  5.1
    1  object2  1.2  2.2  3.2  4.2  5.2
    2  object3  1.3  2.3  3.3  4.3  5.3
    3  object4  1.4  2.4  3.4  4.4  5.4
    4  object5  1.5  2.5  3.5  4.5  5.5
    5  object6  1.6  2.6  3.6  4.6  5.6
    6  object7  1.7  2.7  3.7  4.7  5.7
    

    否则默认直接read_csv(如pd.read_csv('dum.txt'))会返回:

                Type\tT1\tT2\tT3\tT4\tT5
    0  Tag\tGood\tGood\tGood\tGood\tGood
    1   object1\t1.1\t2.1\t3.1\t4.1\t5.1
    2   object2\t1.2\t2.2\t3.2\t4.2\t5.2
    3   object3\t1.3\t2.3\t3.3\t4.3\t5.3
    4   object4\t1.4\t2.4\t3.4\t4.4\t5.4
    5   object5\t1.5\t2.5\t3.5\t4.5\t5.5
    6   object6\t1.6\t2.6\t3.6\t4.6\t5.6
    7   object7\t1.7\t2.7\t3.7\t4.7\t5.7
    8   object8\t1.8\t2.8\t3.8\t4.8\t5.8
    

    【讨论】:

    • U9-Forward:非常感谢,您的评论很有帮助。 =)
    • U9-Forward:我必须再等一分钟才能接受任何答案。我看到这条消息You can accept an answer in 1 more minute. ;)
    • @LingGuo 然后等一下:-)
    • U9-Forward:我赞成您的回答,以及您所有(和 sacul 的)cmets。我这边没有任何反对意见。你和 sacul 是最有帮助的。你有没有看到任何反对意见?
    • @LingGuo 我做了,但不知道为什么? :-) 谢谢你 :-)
    猜你喜欢
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多