简体   繁体   中英

Change output format of Pandas dictionary in Python?

I have a csv file containing all the fruits data

import pandas as pd
df = pd.read_csv('fruit_list.csv')

grouped_by_places = df.groupby('Places')
sort_quad = grouped_by_places['Quality']
max_quad = sort_quad.max()

But when i do print(max_quad.to_dict()), i can only get the default output format for dictionary

{'A': 100, 'B': 70, 'C': 99, 'D': 84, 'E': 98, 'F': 89}

But i am trying to print the output as

A : 100.0
B : 70.0
C : 99.0
D : 84.0
E : 98.0
F : 89.0

Please help thanks for reading.

Use Series.items with print and f-string s:

for k,v in max_quad.items():
    print (f'{k} : {v}')
    
    A : 100
    B : 70
    C : 99
    D : 84
    E : 98
    F : 89

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM