Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 在Python中通过列标题引用表列_Python 2.7 - Fatal编程技术网

Python 2.7 在Python中通过列标题引用表列

Python 2.7 在Python中通过列标题引用表列,python-2.7,Python 2.7,是否有一种按名称引用2D列表列的python方法 我从网上导入了很多表格,所以我做了一个通用函数,用各种HTML表格创建二维列表。到现在为止,一直都还不错。但下一步通常是逐行解析表 # Sample table. # In real life I would do something like: table = HTML_table('url', 'table id') table = [ ['Column A', 'Column B', 'Column C'], ['One

是否有一种按名称引用2D列表列的python方法

我从网上导入了很多表格,所以我做了一个通用函数,用各种HTML表格创建二维列表。到现在为止,一直都还不错。但下一步通常是逐行解析表

# Sample table. 
# In real life I would do something like: table = HTML_table('url', 'table id')
table = 
[
    ['Column A', 'Column B', 'Column C'],
    ['One', 'Two', 3],
    ['Four', 'Five', 6]
]

# Current code:
iA = table[0].index('Column A')
iB = tabel[0].index('Column B')
for row in table[1:]:
    process_row(row[iA], row[iC])

# Desired code:
for row in table[1:]:
    process_row(row['Column A'], row['Column C'])

我想你会很喜欢熊猫模块的

将列表放入数据框中 这也可以直接从html、csv等中完成

df = pd.DataFrame(table[1:], columns=table[0]).astype(str)
访问列 按索引访问第一行 逐行处理 处理列
对于您的问题,将键排序为列名称和值以及行列表不是更好的方法吗?我会选择这样的方式:

table = {
    'Column A': [1, 4],
    'Column B': [2, 5],
    'Column C': [3, 6]
}

# And you would parse column by column...

for col, rows in table.iteritems():
    #do something

我的查询列表使用起来很简单

ql.filter(公文包='123')

ql.group_by(['portfolio','ticker']))

我试过熊猫。我想喜欢它,我真的喜欢。但最终它对我的需求来说太复杂了

例如:

df[df['portfolio']=='123']&df['ticker']=='MSFT']]

并没有那么简单

ql.filter(portfolio='123',ticker='MSFT')

此外,创建QueryList比创建df更简单


这是因为您倾向于使用带有查询列表的自定义类。数据转换代码自然会被放入自定义类中,该类将其与逻辑的其余部分分开。但是df的数据转换通常会与代码的其余部分内联完成。

pandas模块是否能很好地处理表格中的文本,还是主要用于数字?(我对问题进行了编辑,将文本包含在表格中,以显示代表性数据)。不,这是混合数据的理想选择。数字、字符串、日期等等!将其视为内存中的数据库。我会在几分钟内用熊猫版的问题编辑我的答案听起来很酷!熊猫相当于什么:
对于表[1]中的行:
处理_行(row['Column A'],row['Column C'])
?我在“逐行处理”下的“逐行处理”下添加了您的问题的答案,不是吗:
处理_行(row['Column A'],row['Column C'])
?我应该补充一点,这是一个示例实现,只是一个dict,不是有秩序的。
df.iloc[0]
df.apply(lambda x: '_'.join(x), axis=0)

for index,row in df.iterrows():
    process_row(row['Column A'], row['Column C'])
df['Column C'].astype(int).sum()
table = {
    'Column A': [1, 4],
    'Column B': [2, 5],
    'Column C': [3, 6]
}

# And you would parse column by column...

for col, rows in table.iteritems():
    #do something
class QueryList(list):
    """filter and/or group_by a list of objects."""

    def group_by(self, attrs) -> dict:
        """Like a database group_by function.

        args:
            attrs: str or list.

        Returns:
            {value_of_the_group: list_of_matching_objects, ...}
            When attrs is a list, each key is a tuple.
            Ex:
            {'AMZN': QueryList(),
            'MSFT': QueryList(),
            ...
            }
            -- or --
            {('Momentum', 'FB'): QueryList(),
             ...,
            }
        """
        result = defaultdict(QueryList)
        if isinstance(attrs, str):
            for item in self:
                result[getattr(item, attrs)].append(item)
        else:
            for item in self:
                result[tuple(getattr(item, x) for x in attrs)].append(item)

        return result

   def filter(self, **kwargs):
        """Returns the subset of IndexedList that has matching attributes.
        args:
            kwargs: Attribute name/value pairs.

        Example:
            foo.filter(portfolio='123', account='ABC').
        """
        ordered_kwargs = OrderedDict(kwargs)
        match = tuple(ordered_kwargs.values())

        def is_match(item):
            if tuple(getattr(item, y) for y in ordered_kwargs.keys()) == match:
                return True
            else:
                return False

        result = IndexedList([x for x in self if is_match(x)])

        return result

    def scalar(self, default=None, attr=None):
        """Returns the first item in this QueryList.

        args:
            default: The value to return if there is less than one item,
                or if the attr is not found.
            attr: Returns getattr(item, attr) if not None.
        """
        item, = self[0:1] or [default]

        if attr is None:
            result = item
        else:
            result = getattr(item, attr, default)
        return result