简体   繁体   English

用部分重叠的y值绘制数据框中列的每个唯一元素

[英]Plot each unique element of a column in a dataframe with partially overlapping y-Values

I hope I can explain this properly as I am new to pandas. 我是熊猫新手,希望我能正确解释这一点。 I have the following dataframe in pandas. 我在熊猫中有以下数据框。

import numpy as np
plant1 = {'Date' : pd.date_range('1/1/2011', periods=10, freq='D'),
     'Plant' : pd.Series(["Plant1"]*10),
     'Output' : pd.Series(abs(np.random.randn(10)))}

plant2 = {'Date' : pd.date_range('1/3/2011', periods=10, freq='D'),
     'Plant' : pd.Series(["Plant2"]*10),
     'Output' : pd.Series(abs(np.random.randn(10)))}

plant3 = {'Date' : pd.date_range('1/5/2011', periods=10, freq='D'),
     'Plant' : pd.Series(["Plant3"]*10),
     'Output' : pd.Series(abs(np.random.randn(10)))}     



df_plant_1 = pd.DataFrame(plant1)
df_plant_2 = pd.DataFrame(plant2)
df_plant_3 = pd.DataFrame(plant3)

sample = pd.concat([df_plant_1,df_plant_2,df_plant_3])

My output is meant to be an area plot with each individual plant and the respective y-Value (Output) and x-value (Date). 我的输出是每个植物的面积图,分别是y值(输出)和x值(日期)。 Notice that "Dates" are only partially overlapping. 请注意,“日期”仅部分重叠。

I am stuck at finding a way to meaningful organize my data. 我一直坚持寻找一种有意义的方式来组织数据的方法。 The first challenge is to merge the data for duplicate "Date"-Values. 第一个挑战是合并重复的“日期”-值的数据。 The next step would be to fill the resulting holes in the series with .fillna(). 下一步将使用.fillna()填充系列中的结果漏洞。 The final step would be to plot for each unique "Plant"-value. 最后一步是绘制每个唯一的“ Plant”值。

However, I am already stuck at the first step. 但是,我已经停留在第一步。 I am aware of the .merge function, but don't know how to apply it to this case. 我知道.merge函数,但是不知道如何将其应用于这种情况。

Thank you for your time and consideration. 感谢您的时间和考虑。

A user in the chat made me aware of the pivot-function. 聊天中的一个用户使我意识到了枢轴功能。

test = pd.pivot_table(sample, index='Date', columns='Plant', values='Output')
test = test.fillna(method='pad')                            
test = test.fillna(method='bfill')        

plt.figure(); test.plot(kind='area')

在此处输入图片说明

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

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