2017-06-21 77 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