本文整理汇总了Python中journalism.Table.where方法的典型用法代码示例。如果您正苦于以下问题:Python Table.where方法的具体用法?Python Table.where怎么用?Python Table.where使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类journalism.Table
的用法示例。
在下文中一共展示了Table.where方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_where_preserves_rows
# 需要导入模块: from journalism import Table [as 别名]
# 或者: from journalism.Table import where [as 别名]
def test_where_preserves_rows(self):
table = Table(self.rows, self.column_types, self.column_names)
table2 = table.where(lambda r: r['one'] == 1)
table3 = table2.where(lambda r: r['one'] == 1)
self.assertIsNot(table._data[0], table2._data[0])
self.assertIs(table2._data[0], table3._data[0])
示例2: test_where
# 需要导入模块: from journalism import Table [as 别名]
# 或者: from journalism.Table import where [as 别名]
def test_where(self):
table = Table(self.rows, self.column_types, self.column_names)
new_table = table.where(lambda r: r['one'] in (2, None))
self.assertIsNot(new_table, table)
self.assertEqual(len(new_table.rows), 2)
self.assertSequenceEqual(new_table.rows[0], (2, 3, 'b'))
self.assertSequenceEqual(new_table.columns['one'], (2, None))
示例3: open
# 需要导入模块: from journalism import Table [as 别名]
# 或者: from journalism.Table import where [as 别名]
with open('examples/realdata/Datagov_FY10_EDU_recp_by_State.csv') as f:
# Skip headers
next(f)
next(f)
next(f)
rows = list(csv.reader(f))
# Trim cruft off end
rows = rows[:-2]
# Create the table
table = Table(rows, COLUMN_TYPES, COLUMN_NAMES)
# Remove Phillipines and Puerto Rico
states = table.where(lambda r: r['state_abbr'] not in ('PR', 'PH'))
# Sum total of all states
print('Total of all states: %i' % states.columns['total'].sum())
# Sort state total, descending
order_by_total_desc = states.order_by('total', reverse=True)
# Grab just the top 5 states
top_five = order_by_total_desc.rows[:5]
for i, row in enumerate(top_five):
print('# %i: %s %i' % (i, row['state'], row['total']))
with open('sorted.csv', 'w') as f:
writer = csv.writer(f)