正如评论中已经强调的那样,pandas 不支持“子数据帧”。为了 KISS,我建议复制这些行(或者管理两个单独的表......如果确实有必要)。
您提到的问题的答案(将 pandas 数据框单元格中的字典解析为新的行单元格(新列) https://stackoverflow.com/questions/39640936/parsing-a-dictionary-in-a-pandas-dataframe-cell-into-new-row-cells-new-columns) 导致new(框架宽)列each(行本地)“计算机名称”。考虑到您的领域模型,我怀疑这是否是您的目标。
pandas 的缩写可以通过使用其他输出引擎来规避,例如tabulate https://pypi.org/project/tabulate/ (漂亮地打印 pandas 数据框 https://stackoverflow.com/questions/18528533/pretty-printing-a-pandas-dataframe):
# standard pandas output
Vendor BIOS Version Newest BIOS Against M & S W10 Support Computer Location ... Category4 Category5 Category6 Category7 Category8 Category9 Category0
0 Dell Inc. 12.72.9 12.73.9 Yes Yes someName001 12.72.9 ... SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory
1 Dell Inc. 12.72.9 12.73.9 Yes Yes someName002 12.73.9 ... SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory
2 Dell Inc. 12.73.9 12.73.9 Yes Yes someName003 12.73.9 ... SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory
[3 rows x 17 columns]
# tabulate psql (with headers)
+----+------------+----------------+---------------+-----------------+---------------+-------------+------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| | Vendor | BIOS Version | Newest BIOS | Against M & S | W10 Support | Computer | Location | Category1 | Category2 | Category3 | Category4 | Category5 | Category6 | Category7 | Category8 | Category9 | Category0 |
|----+------------+----------------+---------------+-----------------+---------------+-------------+------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------|
| 0 | Dell Inc. | 12.72.9 | 12.73.9 | Yes | Yes | someName001 | 12.72.9 | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory |
| 1 | Dell Inc. | 12.72.9 | 12.73.9 | Yes | Yes | someName002 | 12.73.9 | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory |
| 2 | Dell Inc. | 12.73.9 | 12.73.9 | Yes | Yes | someName003 | 12.73.9 | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory |
+----+------------+----------------+---------------+-----------------+---------------+-------------+------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
# tabulate psql
+---+------------+---------+---------+-----+-----+-------------+---------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| 0 | Dell Inc. | 12.72.9 | 12.73.9 | Yes | Yes | someName001 | 12.72.9 | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory |
| 1 | Dell Inc. | 12.72.9 | 12.73.9 | Yes | Yes | someName002 | 12.73.9 | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory |
| 2 | Dell Inc. | 12.73.9 | 12.73.9 | Yes | Yes | someName003 | 12.73.9 | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory | SomeCategory |
+---+------------+---------+---------+-----+-----+-------------+---------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
# tabulate plain
Vendor BIOS Version Newest BIOS Against M & S W10 Support Computer Location Category1 Category2 Category3 Category4 Category5 Category6 Category7 Category8 Category9 Category0
0 Dell Inc. 12.72.9 12.73.9 Yes Yes someName001 12.72.9 SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory
1 Dell Inc. 12.72.9 12.73.9 Yes Yes someName002 12.73.9 SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory
2 Dell Inc. 12.73.9 12.73.9 Yes Yes someName003 12.73.9 SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory SomeCategory
你也可以使用一些groupBy(..).apply(..)
+ string magic 生成一个字符串表示形式,它只是隐藏重复项:
# tabulate + merge manually
+----+--------------+------------+----------------+---------------+-----------------+---------------+-------------+------------+--------------+--------------+
| | Type | Vendor | BIOS Version | Newest BIOS | Against M & S | W10 Support | Computer | Location | Category1 | Category2 |
|----+--------------+------------+----------------+---------------+-----------------+---------------+-------------+------------+--------------+--------------|
| 0 | HexaPlex x50 | Dell Inc. | 12.72.9 | 12.73.9 | Yes | Yes | someName001 | 12.72.9 | SomeCategory | SomeCategory |
| | | | 12.72.9 | | | | someName002 | 12.73.9 | | |
| | | | 12.73.9 | | | | someName003 | 12.73.9 | | |
+----+--------------+------------+----------------+---------------+-----------------+---------------+-------------+------------+--------------+--------------+
样式输出可以通过新的生成样式API https://pandas.pydata.org/pandas-docs/stable/style.html仍然是临时和正在开发中:
同样,您可以使用一些逻辑来“合并”列中连续的冗余值(简单的示例,我假设更多的努力可能会产生更好的输出):
Code对于上面的例子
import pandas as pd
from tabulate import tabulate
import functools
def pprint(df, headers=True, fmt='psql'):
# https://stackoverflow.com/questions/18528533/pretty-printing-a-pandas-dataframe
print(tabulate(df, headers='keys' if headers else '', tablefmt=fmt))
df = pd.DataFrame({
'Type': ['HexaPlex x50'] * 3,
'Vendor': ['Dell Inc.'] * 3,
'BIOS Version': ['12.72.9', '12.72.9', '12.73.9'],
'Newest BIOS': ['12.73.9'] * 3,
'Against M & S': ['Yes'] * 3,
'W10 Support': ['Yes'] * 3,
'Computer': ['someName001', 'someName002', 'someName003'],
'Location': ['12.72.9', '12.73.9', '12.73.9'],
'Category1': ['SomeCategory'] * 3,
'Category2': ['SomeCategory'] * 3,
'Category3': ['SomeCategory'] * 3,
'Category4': ['SomeCategory'] * 3,
'Category5': ['SomeCategory'] * 3,
'Category6': ['SomeCategory'] * 3,
'Category7': ['SomeCategory'] * 3,
'Category8': ['SomeCategory'] * 3,
'Category9': ['SomeCategory'] * 3,
'Category0': ['SomeCategory'] * 3,
})
print("# standard pandas print")
print(df)
print("\n# tabulate tablefmt=psql (with headers)")
pprint(df)
print("\n# tabulate tablefmt=psql")
pprint(df, headers=False)
print("\n# tabulate tablefmt=plain")
pprint(df, fmt='plain')
def merge_cells_for_print(rows, ls='\n'):
result = pd.DataFrame()
for col in rows.columns:
vals = rows[col].values
if all([val == vals[0] for val in vals]):
result[col] = [vals[0]]
else:
result[col] = [ls.join(vals)]
return result
print("\n# tabulate + merge manually")
pprint(df.groupby('Type').apply(merge_cells_for_print).reset_index(drop=True))
# https://pandas.pydata.org/pandas-docs/stable/style.html
# https://pandas.pydata.org/pandas-docs/version/0.22.0/generated/pandas.io.formats.style.Styler.apply.html#pandas.io.formats.style.Styler.apply
def highlight_lower(ref, col):
return [f'color: {"red" if hgl else ""}' for hgl in col < ref]
def merge_duplicates(col):
vals = col.values
return [''] + ['color: transparent' if curr == pred else '' for pred, curr in zip(vals[1:], vals)]
with open('only_red.html', 'w+') as f:
style = df.style
style = style.apply(functools.partial(highlight_lower, df['Newest BIOS']),
subset=['BIOS Version'])
f.write(style.render())
with open('red_and_merged.html', 'w+') as f:
style = df.style
style = style.apply(functools.partial(highlight_lower, df['Newest BIOS']),
subset=['BIOS Version'])
style = style.apply(merge_duplicates)
f.write(style.render())