2017-05-06 234 views
1

我想这必须与缺乏单位/我的单位不连续编号的事实有关。tsplot不再密谋(再次)

我有一个在不同年份测量的批次ID的DF,但许多批次ID只在一年,也许两个功能。

我还想随时间绘制样本的发展情况。

任何想法如何解决它? df.to_dict()是here

import pandas as pd 
import numpy as np 
import seaborn as sns 
import matplotlib.pyplot as plt 
sns.tsplot(df, time='Year', value='Quality', unit='BatchID', condition='Vegetable') 

回答

2

是的,有问题你的时间行丢失。

一个可能的解决方案是groupby通过BatchID和范围reindexmin创建和maxYear和遗漏值都充满了ffillbffil什么是相同fillna方法:

所以从:

print (df.head(3)) 
    BatchID  Farmer Quality Vegetable Year 
0  31 Pepperidge  0.55 Potato  9 
1  31 Pepperidge  0.80 Potato 10 
2  95  Johnson  0.50 Carrot  6 

得到这个24行:

df1 = df.groupby('BatchID') 
     .apply(lambda x: x.set_index('Year') 
          .reindex(range(df.Year.min(), df.Year.max() + 1)).ffill().bfill()) 
     .reset_index(level=1) 
     .reset_index(drop=True) 

print (df1.head(24)) 
    Year BatchID  Farmer Quality Vegetable 
0  1  31.0 Pepperidge  0.55 Potato 
1  2  31.0 Pepperidge  0.55 Potato 
2  3  31.0 Pepperidge  0.55 Potato 
3  4  31.0 Pepperidge  0.55 Potato 
4  5  31.0 Pepperidge  0.55 Potato 
5  6  31.0 Pepperidge  0.55 Potato 
6  7  31.0 Pepperidge  0.55 Potato 
7  8  31.0 Pepperidge  0.55 Potato 
8  9  31.0 Pepperidge  0.55 Potato 
9  10  31.0 Pepperidge  0.80 Potato 
10 11  31.0 Pepperidge  0.80 Potato 
11 12  31.0 Pepperidge  0.80 Potato 
12  1  95.0  Johnson  0.50 Carrot 
13  2  95.0  Johnson  0.50 Carrot 
14  3  95.0  Johnson  0.50 Carrot 
15  4  95.0  Johnson  0.50 Carrot 
16  5  95.0  Johnson  0.50 Carrot 
17  6  95.0  Johnson  0.50 Carrot 
18  7  95.0  Johnson  0.50 Carrot 
19  8  95.0  Johnson  0.50 Carrot 
20  9  95.0  Johnson  0.50 Carrot 
21 10  95.0  Johnson  0.50 Carrot 
22 11  95.0  Johnson  0.50 Carrot 
23 12  95.0  Johnson  0.50 Carrot 


sns.tsplot(df1, time='Year', value='Quality', unit='BatchID', condition='Vegetable') 

graph

+0

这是不幸的是不准确的,因为我没有这个信息,这将扭曲的结果。每个蔬菜和每年大约有300个测量值,但是batchID不同。 –

+0

我不确定是否理解 - 如果某个'batchID'的'Year'中缺少值,则不会绘图。所以'tsplot'是不可能使用的。 – jezrael

+0

另一方面,并​​非所有BatchID都存在于每年。所以如果tsplot不能被使用,是他们的另一种情节选择,而不仅仅是寻找手段,并且将我的自我标准化,并将其绘制出来? –