2017-07-03 70 views
2

我有一個數據集採用如何從數據透視表數據框列註釋圖表?

a b c d 
10-Apr-86 Jimmy 1 this is 
11-Apr-86 Minnie 2 the way 
12-Apr-86 Jimmy 3 the world 
13-Apr-86 Minnie 4 ends 
14-Apr-86 Jimmy 5 this is the 
15-Apr-86 Eliot 6 way 
16-Apr-86 Jimmy 7 the world ends 
17-Apr-86 Eliot 8 not with a bang 
18-Apr-86 Minnie 9 but a whimper 

我想打一個圖表中matplotlib,看起來像這樣

enter image description here

我已經計算出如何得到公正的點(沒有註釋)下面的代碼:

df = (pd.read_csv('python.csv')) 
df_wanted = pd.pivot_table(
    df, 
    index='a', 
    columns='b', 
    values='c') 

df_wanted.index = pd.to_datetime(df_wanted.index) 

plt.scatter(df_wanted.index, df_wanted['Jimmy']) 
plt.scatter(df_wanted.index,df_wanted['Minnie']) 
plt.scatter(df_wanted.index,df_wanted['Eliot']) 

我認爲要註釋,我需要一個值的列表(如演示here),我的數據透視表的最後一欄

我的問題是:我怎麼獲取原始數據集的最後一列「d」成爲我的數據透視表的最後一欄?

我試圖dat1 = pd.concat([df_wanted, df['d']], axis = 1) - 但這建立了一套新的數據框我的行下面的行。我意識到軸是不一樣的,所以我試圖做一個新的數據透視表與d列值 - 但得到錯誤信息No numeric types to aggregate

我試圖df_wanted2.append(df['d']) - 但是這提出了一個新列列d每個元素。

有什麼建議嗎?最後,我希望把它使數據標籤出現在一個翻轉鼠標

回答

1

在這種特定的情況來看,它似乎並不需要設置列d爲您的數據透視表的最後一欄。

plt.scatter(df_wanted.index, df_wanted['Jimmy']) 
plt.scatter(df_wanted.index,df_wanted['Minnie']) 
plt.scatter(df_wanted.index,df_wanted['Eliot']) 
plt.legend(loc=0) 

for k, v in df.set_index('a').iterrows(): 
    plt.text(k, v['c'], v['d']) # or: plt.annotate(xy=(k, v['c']), s=v['d']) 

enter image description here

相關問題