python:逐行打印多个字典和字符串
python: print multiple dictionaries and strings line by line
我有一个包含各种列的数据框,我计算了每一列的 value_counts 并将它们转换为 to_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') 但它只 returns 索引并删除数字(计数)
试试这个,
import json
print( 'Names and counts',
'\n',
json.dumps(df['names'].value_counts()[:3].to_dict(),indent=2),
'\n',
'Last names and counts',
'\n',
json.dumps(df['last names'].value_counts()[:3].to_dict(),indent=2),
'Staff names and counts',
'\n',
json.dumps(df['staff_names'].value_counts()[:3].to_dict(),indent=2),
'\n',
'Staff last names and counts',
'\n',
json.dumps(df['staff last names'].value_counts()[:3].to_dict(),indent=2)
将缩进参数更改为更大的数字以获得更好的格式..
Note: Tested by Creator and working fine.
使用 json.dumps
和 indent
= 字典级别:
for c in df.columns:
print(f"{c} and counts\n{json.dumps(df[c].value_counts()[:3].to_dict(),indent=2)}")
我有一个包含各种列的数据框,我计算了每一列的 value_counts 并将它们转换为 to_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') 但它只 returns 索引并删除数字(计数)
试试这个,
import json
print( 'Names and counts',
'\n',
json.dumps(df['names'].value_counts()[:3].to_dict(),indent=2),
'\n',
'Last names and counts',
'\n',
json.dumps(df['last names'].value_counts()[:3].to_dict(),indent=2),
'Staff names and counts',
'\n',
json.dumps(df['staff_names'].value_counts()[:3].to_dict(),indent=2),
'\n',
'Staff last names and counts',
'\n',
json.dumps(df['staff last names'].value_counts()[:3].to_dict(),indent=2)
将缩进参数更改为更大的数字以获得更好的格式..
Note: Tested by Creator and working fine.
使用 json.dumps
和 indent
= 字典级别:
for c in df.columns:
print(f"{c} and counts\n{json.dumps(df[c].value_counts()[:3].to_dict(),indent=2)}")