【问题标题】:Python Pandas Data frame to subplot dictionaryPython Pandas 数据框到子图字典
【发布时间】:2022-01-02 18:35:09
【问题描述】:

我正在尝试在 matplotlib 中使用子图。

我有一个如下所示的数据框:

FY_YYYY_Q Gender Head Count
0 2017 Q4 Female 220
1 2017 Q4 Male 200

我需要将它作为字典传递给 plots 参数:

{'2017 Q4': {'Values': 220, 200}, {'Labels': Female, Male}}

对 Python 很陌生,所以我不知道该怎么做。任何帮助,将不胜感激。提前致谢!

【问题讨论】:

    标签: python dataframe dictionary matplotlib


    【解决方案1】:

    首先,你的字典的定义是非常错误的,正确的形式应该是这样的:

    dict = {key: value}
    # so in your case I fixed it to be like this: 
    d = {'2017 Q4': ({'Values': (220, 200)}, {'Labels': ("Female", "Male")})}
    # and this is still very counter intuitive, so use this instead:
    d = {'2017 Q4':(220,200)}  
    # and just make it so in the code the first value is female and the next is male
    

    至于如何从字典中绘制图表,请使用以下代码:

    a_dictionary = {"values": (220,210), "labels": ("female","male")}
    
    labels = a_dictionary["labels"]
    
    values = a_dictionary["values"]
    
    plt.bar(labels, values)
    

    【讨论】:

    • 谢谢。问题是,我需要某种函数将数据帧转换为格式为 {'2017 Q4': ({'Values': (220, 200)}, {'Labels': ("Female", "男性”)})}。每年季度都会有一个情节。
    猜你喜欢
    • 2013-09-12
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2023-03-13
    • 2017-10-15
    相关资源
    最近更新 更多