简体   繁体   English

需要修复熊猫数据框的输出

[英]Need to fix output from Pandas data frame

The program works great but the output is just not in the format i want. 该程序效果很好,但输出结果不是我想要的格式。 As you can se from the output below, the printout for the 'from' and 'to' data is not at the same level as the printout for 'route' and 'date'. 从下面的输出中可以看出,“从”和“到”数据的打印输出与“路由”和“日期”的打印输出不在同一级别。 How can this be fixed? 如何解决? I want the output from date,route,from and to on the same level. 我希望从date,route,from和到同一级别的输出。

      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

Also, this program is supposed to iterate through any number of .txt files. 另外,该程序应迭代任意数量的.txt文件。 But it only works when there is just one text file in the target directory. 但是,仅当目标目录中只有一个文本文件时,它才有效。

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()

I had the same problem with too narrow output and solved it with putting ( import pandas as pd is assumed) 我在输出太窄时也遇到了同样的问题,并通过推入解决了问题(假设import pandas as pd

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

somewhere in the beginning of the file. 文件开头的某个位置。 Just for completeness I will also mention 为了完整起见,我还会提到

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

For more options and examples see pandas documentation 有关更多选项和示例,请参见pandas文档

As for not iterating through *.txt files, can it be that your "for col" loop just needs extra indentation ? 至于不遍历* .txt文件,难道是您的“ for col”循环仅需要额外的缩进吗? That's how it looks to me with your indentations as they are displayed in your post. 在您的帖子中显示您的缩进时,这就是我的样子。 Your "for file" loop is finished and only then your "for col" loop begins. 您的“ for文件”循环完成,然后才开始“ for col”循环。

By the way, the indentations inside both for-loops are displayed as double indentations, which should not be. 顺便说一下,两个for循环中的缩进都显示为双缩进,不应显示为双缩进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM