简体   繁体   English

加入两个Pandas数据框并生成并列条形图?

[英]Joining two Pandas dataframes and producing side-by-side barplot?

Suppose I have two Pandas dataframes, df1 and df2 , each with two columns, hour and value . 假设我有两个Pandas数据帧df1df2 ,每个数据帧都有两列,分别是hourvalue Some of the hours are missing in the two dataframes. 两个数据框中缺少一些时间。

import pandas as pd
import matplotlib.pyplot as plt
data1 = [
    ('hour', [0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12,
              13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]),
    ('value', [12.044324085714285, 8.284134466666668, 9.663580800000002,
               18.64010145714286, 15.817029916666664, 13.242198508695651,
               10.157177889201877, 9.107153674476985, 10.01193336545455,
               16.03340384878049, 16.037368506666674, 16.036160044827593,
               15.061596637500001, 15.62831551764706, 16.146087032608694,
               16.696574719512192, 16.02603831463415, 17.07469460470588,
               14.69635686969697, 16.528905725581396, 12.910250661111112,
               13.875522341935481, 12.402971938461539])
    ]

df1 = pd.DataFrame.from_items(data1)
df1.head()
#    hour      value
# 0     0  12.044324
# 1     1   8.284134
# 2     2   9.663581
# 3     4  18.640101
# 4     5  15.817030

data2 = [
    ('hour', [0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
              15, 16, 17, 18, 19, 20, 21, 22, 23]),
    ('value', [27.2011904, 31.145661266666668, 27.735570511111113,
               18.824297487999996, 17.861847334275623, 25.3033003254902,
               22.855934450000003, 31.160574200000003, 29.080220000000004,
               30.987719745454548, 26.431310216666663, 30.292641480000004,
               27.852885586666666, 30.682682472727276, 29.43023531764706,
               24.621718962500005, 33.92878745, 26.873105866666666,
               34.06412232, 32.696606333333335])
    ]

df2 = pd.DataFrame.from_items(data2)
df2.head()
#    hour      value
# 0     0  27.201190
# 1     5  31.145661
# 2     6  27.735571
# 3     7  18.824297
# 4     8  17.861847

I would like to join them together using the key of hour and then produce a side-by-side barplot of the data. 我想使用hour键将它们连接在一起,然后生成数据的并排条形图。 The x-axis would be hour , and the y-axis would be value . x轴为houry-axisvalue

I can create a bar plot of one dataframe at a time. 我可以一次创建一个数据框的条形图。

_ = plt.bar(df1.hour.tolist(), df1.value.tolist())
_ = plt.xticks(df1.hour, rotation=0)
_ = plt.grid()
_ = plt.show()

在此处输入图片说明

_ = plt.bar(df2.hour.tolist(), df2.value.tolist())
_ = plt.xticks(df2.hour, rotation=0)
_ = plt.grid()
_ = plt.show()

在此处输入图片说明

However, what I want is to create a barchart of them side by side, like this: 但是,我想要的是并排创建它们的条形图,如下所示:

在此处输入图片说明

Thank you for any help. 感谢您的任何帮助。

First reindex the dataframes and then create two barplots using the data. 首先为数据框重新索引,然后使用数据创建两个条形图。 The positioning of the rectangles is given by (x - width/2, x + width/2, bottom, bottom + height) . 矩形的位置由(x - width/2, x + width/2, bottom, bottom + height)

import numpy as np

index = np.arange(0, 24)
bar_width = 0.3

df1 = df1.set_index('hour').reindex(index)
df2 = df2.set_index('hour').reindex(index)

plt.figure(figsize=(10, 5))
plt.bar(index - bar_width / 2, df1.value, bar_width, label='df1')
plt.bar(index + bar_width / 2, df2.value, bar_width, label='df2')
plt.xticks(index)
plt.legend()

plt.tight_layout()
plt.show()

情节

You can do it all in one line, if you wish. 如果您愿意,您可以一站式完成所有操作。 Making use of the pandas plotting wrapper and the fact that plotting a dataframe with several columns will group the plot. 利用pandas绘图包装器和绘制具有几列的数据框将对绘图进行分组的事实。 Given the definitions of df1 and df2 from the question, you can call 给定问题中df1df2的定义,您可以调用

pd.merge(df1,df2, how='outer', on=['hour']).set_index("hour").plot.bar()
plt.show()

resulting in 导致

在此处输入图片说明

Note that this leaves out the number 3 in this case as it is not part of any hour column in any of the two dataframes. 请注意,在这种情况下,这将省略数字3,因为它不是两个数据帧中任何一个小时列的一部分。 To include it, use reset_index 要包含它,请使用reset_index

pd.merge(df1,df2, how='outer', on=['hour']).set_index("hour").reindex(range(24)).plot.bar()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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