2017-06-21 67 views
1

考虑一个简单的2x2的数据集与系列标签前缀作为第一列(“回购”)推断系列标签和数据从大熊猫数据框中列绘制

  Repo AllTests Restricted 
0  Galactian 1860.0  410.0 
1 Forecast-MLib 140.0  47.0 

下面是数据框列:

p(df.columns) 
([u'Repo', u'AllTests', u'Restricted'] 

所以我们第一列是字符串/标签,第二列和第三列是数据值。我们希望对应于GalactianForecast-MLlib回购的每一个系列。

看来这将是一个常见的任务,并且会有一个简单的方法来简单地DataFrame的plot。然而,以下相关的问题不提供任何简单的方法:它基本上是手工扔掉数据帧结构的知识和情节: Set matplotlib plot axis to be the dataframe column name

那么,有没有绘制这些系列更自然的方式 - 不涉及解构already-有用的DataFrame,而是推断第一列作为标签,其余的作为系列数据点?

更新这里是一个自包含片段

runtimes = npa([1860.,410.,140.,47.]) 
runtimes.shape = (2,2) 

labels = npa(['Galactian','Forecast-MLlib']) 
labels.shape=(2,1) 
rtlabels = np.concatenate((labels,runtimes),axis=1) 
rtlabels.shape = (2,3) 

colnames = ['Repo','AllTests','Restricted'] 
df = pd.DataFrame(rtlabels, columns=colnames) 
ps(df) 
df.set_index('Repo').astype(float).plot() 
plt.show() 

这里是输出

   Repo AllTests Restricted 
0  Galactian 1860.0  410.0 
1 Forecast-MLlib 140.0  47.0 

而且随着piRSquared帮助它看起来像这样

enter image description here 因此数据显示现在..但是系列和标签交换。将进一步尝试将它们正确排列。

另一个更新

通过flipping the columns/labels串联正出来如所期望。

的变化是:

labels = npa(['AllTests','Restricted']) 
.. 
colnames = ['Repo','Galactian','Forecast-MLlib'] 

enter image description here

所以更新的代码是

runtimes = npa([1860.,410.,140.,47.]) 
runtimes.shape = (2,2) 

labels = npa(['AllTests','Restricted']) 
labels.shape=(2,1) 
rtlabels = np.concatenate((labels,runtimes),axis=1) 
rtlabels.shape = (2,3) 

colnames = ['Repo','Galactian','Forecast-MLlib'] 
df = pd.DataFrame(rtlabels, columns=colnames) 
ps(df) 
df.set_index('Repo').astype(float).plot() 
plt.title("Restricting Long-Running Tests\nin Galactus and Forecast-ML") 
plt.show() 

p('df columns', df.columns) 
ps(df) 

回答

1

熊猫假设你的标签信息是在索引和列。首先设置索引:

df.set_index('Repo').astype(float).plot() 

或者

df.set_index('Repo').T.astype(float).plot() 
+0

THX!我结束了'TypeError:空'DataFrame':没有数字数据来绘制'。如果第二个和第三个(仅限数字)列使用'df.astype(float)'转换,则会出现一个图形,但是没有标签/索引。 – javadba

+0

@javadba在set_index后放置astype – piRSquared

+0

请参阅OP:您的建议现在有所帮助。我仍然有倒序的系列和标签。 – javadba