2016-02-17 24 views
3

我想使画面就像下面:Seaborn tsplot:“索引包含重复的条目,不能重塑”尽管指数没有重复

enter image description here

我想用seaborn使图看起来很漂亮,并且使我更容易使用facetgrids(我有十个不同的数据集,我想在同一个图表中显示)。

我在seaborn找到的最接近的东西是tsplots

我有数据,如下所示(500万行):

Bin_nb Sample   Type Count 
0  131 Exp1   Input  1 
1  79 Exp2 Polymerase_II  1 
2  100 Exp1   Input  2 
3  173 Exp2   Input  3 
4  40 Exp1   Input  1 

Bin_nb范围为0〜200,我假装它们所代表的时间。

有在索引中没有重复:

len(df.index.drop_duplicates()) # 5e6 

尽管如此,我得到的错误ValueError: Index contains duplicate entries, cannot reshape

当我尝试的命令

sns.tsplot(data=df, time="Bin_nb", unit="Sample", value="Count", condition="Type") 

版本信息:

>>> sns.__version__ 
'0.6.0' 
>>> pd.__version__ 
'0.17.1' 
+0

升级到seaborn'0.7'和错误仍然存​​在。 –

回答

2

重复的索引大概是Bin_nb Sample Type元组,其中有几个,每个都有其自己的Count值。分组和总结这些固定它!

rowdicts = [] 

for l, d in df.groupby("Bin_nb Sample Type".split()): 
    d = {"Bin_nb": l[0], "Sample": l[1], "Type": l[2]} 
    rowdicts.append(d)        

df2 = pd.DataFrame.from_dict(rowdicts) 

现在的数据是这样的:

 Bin_nb Count Sample   Type 
0   0 11118 Exp1   Input 
1   0 10774 Exp1 Polymerase_II 
2   0 8909 Exp2   Input 
3   0 13704 Exp2 Polymerase_II 
4   1 10388 Exp1   Input 
5   1 11108 Exp1 Polymerase_II 

就万事大吉了绘制的作品!

sns.tsplot(data=df2, time="Bin_nb", unit="Sample", value="Count", condition="Type") 

enter image description here