需要修复Pandas数据帧的输出

2024-10-02 14:19:11 发布

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

程序运行得很好,但是输出的格式不是我想要的。从下面的输出可以看出,“from”和“to”数据的打印输出与“route”和“date”数据的打印输出不在同一级别。如何修复?我希望从date、route、from和to的输出在同一级别上。你知道吗

      date    route
  0  05/15/2014  C000001     from     to
  0  278.7  278.6
      date    route
  0  05/15/2014  C000001     from     to
  0  278.6  278.5

此外,这个程序应该遍历任意数量的.txt文件。但它只在目标目录中只有一个文本文件时才起作用。你知道吗

def array_setter():
    import os
    import glob
    import numpy as np
    import pandas as pd
    os.chdir\
    ('/Users/thomaswolff/Documents/Data Exports')
    for file in glob.glob('*.TXT'):
        reader = open(file)
        headerLine = reader.readline()
        mainBody = reader.readlines()
        for col in mainBody:
            valueList = col.split(",")
            data = np.array([valueList])

            arrstr = data[:,[0,1]]
            arrnum = data[:,[5,6]].astype(np.float)

            dstr = pd.DataFrame(arrstr,columns=['date','route'])
            dnum = pd.DataFrame(arrnum,columns=['from','to'])

            routes = dstr['route'] == 'C000001'
            from_ = dnum['from'] > 278.5

            if routes == True:
                if from_ == True:
                    print dstr,dnum
array_setter()

Tags: to数据fromimportdatadatenparray
1条回答
网友
1楼 · 发布于 2024-10-02 14:19:11

我有同样的问题,输出太窄,解决了它 放置(import pandas as pd假设)

pd.set_option('display.width', 130)    

在文件开头的某个地方。为了完整起见,我还要提到

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 50)    

有关更多选项和示例,请参见pandas documentation

至于不遍历*.txt文件,您的“for col”循环是否只需要额外的缩进?在我看来,这就是你的缩进,因为他们显示在你的职位。“for file”循环完成,只有在“for col”循环开始时。你知道吗

顺便说一下,两个for循环中的缩进都显示为双缩进,不应该这样。你知道吗

相关问题 更多 >