Python-从非数字数据列表创建表

时间:2016-03-20 03:50:20

标签: python pandas matplotlib

我正在寻找一种方法来创建一个简单的表,给出一组3个文本数据列表,以及一个标题列表。这是纲要:我需要3列和不确定数量的行,具体取决于数据源。如果有一种方法可以在Python中正常执行,我也不想导入任何东西。或者,我可以使用pandas,matplotlib或其组合。这是我的代码:

headers=["SCHEME","SERVER","PATH"]
schemes=[]
servers=[]
paths=[]
colLabels=['PATH','SERVER','SCHEME']
rowLabels=[]
def homework2(filename):
    """Opens a file, extracts URLs, parses them, and places them into table"""
    filedata=open(filename,"r")
    for url in filedata:
        lineurl=SimpleURL(url)
        schemes.append(lineurl.scheme())
        servers.append(lineurl.server())
        paths.append(lineurl.path())

此代码读取给定的文本文件,获取每一行,并将URL解析为方案,服务器和路径。三个列表(和标题)如下所示:

['SCHEME', 'SERVER', 'PATH']
['http', 'https', 'https']
['google.com', 'twitter.com', 'github.com']
['/', '/pretend/me', '/asdflkjas/stuff']

我正在寻找创建此输出或类似于

的输出
SCHEME        SERVER         PATH
http          https          https
google.com    twitter.com    github.com
/             /pretend/me    /asdflkjas/stuff

我尝试过使用DataFrame和table()函数,但我无法使其正常工作。

2 个答案:

答案 0 :(得分:0)

<强>代码:

import pandas

headers = ['SCHEME', 'SERVER', 'PATH']
scheme = ['http', 'https', 'https']
server = ['google.com', 'twitter.com', 'github.com']
path = ['/', '/pretend/me', '/asdflkjas/stuff']

data = {}

for i, header in enumerate(headers):
    data.update({header: [scheme[i], server[i], path[i]]})

df = pandas.DataFrame(data=data, columns=headers)
print(df)

<强>输出:

       SCHEME       SERVER              PATH
0        http        https             https
1  google.com  twitter.com        github.com
2           /  /pretend/me  /asdflkjas/stuff

pandas&#39; DataFrame我们可以轻松地做到这一点。

只需将标题和数据连接到字典中,标题就是键,schemeserverpath的列表就是值。

然后只需插入pandas&#39; DataFrame

注意:

如果您希望在更少的代码行中获得相同的输出,您可以将列表连接的行重写为字典,作为一个不太清晰但功能相同的字典理解,如下所示:

data = {header: [scheme[i], server[i], path[i]] for i, header in enumerate(headers)}

答案 1 :(得分:0)

试试这段代码。 column_width是每列中元素的最大长度。

headers = ["SCHEME","SERVER","PATH"]
schemes = ['http', 'https', 'https']
servers = ['google.com', 'twitter.com', 'github.com']
paths = ['/', '/pretend/me', '/asdflkjas/stuff']

def show_URL(headers, schemes, servers, paths):
    column_width = [max(len(x) for x in row) for row in zip(headers, schemes, servers, paths)]
    for item in [headers, schemes, servers, paths]:
        str_printing = ""
        for index, val in enumerate(item):
            str_printing += "{:{}}".format(val, column_width[index] + 5 )
        print (str_printing)

show_URL(headers, schemes, servers, paths)

输出:

SCHEME         SERVER          PATH                 
http           https           https                
google.com     twitter.com     github.com           
/              /pretend/me     /asdflkjas/stuff