【发布时间】:2021-05-26 13:37:36
【问题描述】:
我有一个包含不同列的数据框,我计算了每列的 value_counts 并将它们转换为_dict。我现在想逐行打印它们,并添加一些描述每个字典的字符串,如下所示:
print( 'Names and counts',
'\n',
df['names'].value_counts()[:3].to_dict(),
'\n',
'Last names and counts',
'\n',
df['last names'].value_counts()[:3].to_dict(),
'Staff names and counts',
'\n',
df['staff_names'].value_counts()[:3].to_dict(),
'\n',
'Staff last names and counts',
'\n',
df['staff last names'].value_counts()[:3].to_dict())
Current output:
Names and counts
{"Jack": 20, "John": 10, "Samuel": 9}
Last names and counts
{"Brown": 25, "Smith": 30, "Jackson": 5}
Staff names and counts
{"Mars": 22, "Joshua": 20, "Simon": 8}
Staff last names and counts
{"Bernard": 27, "Kohlen": 16, "Dimun": 7}
所以我希望输出如下所示:
Desired output:
Names and counts
{"Jack": 20,
"John": 10,
"Samuel": 9}
Last names and counts
{"Brown": 25,
"Smith": 30,
"Jackson": 5}
Staff names and counts
{"Mars": 22,
"Joshua": 20,
"Simon": 8}
Staff last names and counts
{"Bernard": 27,
"Kohlen": 16,
"Dimun": 7}
我已经尝试使用 pprint() 或 print() 来代替,但这会引发错误。
我也尝试添加 print(*each dictionary, sep= '\n') 但它只返回索引并删除数字(计数)
【问题讨论】:
-
你当前的输出是多少?
-
当前输出为:一行中的所有字典项
-
好的,我有一个解决方案..
标签: python pandas dictionary printing pprint