2017-10-13 86 views
1

我對大熊貓命名爲「圖」系列,看起來像這樣:選擇欄

Wavelength 
450  37 
455  31 
460  0 
465  0 
470  0 
475  0 
480  418 
485 1103 
490 1236 
495  894 
500  530 
505  85 
510  0 
515  168 
520  0 
525  0 
530  691 
535  842 
540 5263 
545 4738 
550 6237 
555 1712 
560  767 
565  620 
570  0 
575  757 
580 1324 
585 1792 
590  659 
595 1001 
600  601 
605  823 
610  0 
615  134 
620 3512 
625  266 
630  155 
635  743 
640  648 
645  0 
650  583 
Name: A1, dtype: object 

我使用graph.plot()作圖的曲線,它看起來像這樣: enter image description here

目標是平滑曲線。我試圖使用Savgol_Filter,但要做到這一點,我需要將我的系列分成x & y列。到目前爲止,我可以通過使用graph.index來訪問「波長」列,但是我無法抓住下一列來將其指定爲y。

我試過使用iloc和loc,還沒有任何運氣。

任何提示或嘗試新方向?

+0

由於這是一個'Series'你可以用'Y = graph'。 –

回答

3

您無需通過xysavgol_filter。您只需要y值,當您將傳遞給它時,該值將自動通過。缺少的是窗口大小參數和定義平滑的多邊形順序參數。

from scipy.signal import savgol_filter 
import pandas as pd 

# I passed `graph` but I could've passed `graph.values` 
# It is `graph.values` that will get used in the filtering 
pd.Series(savgol_filter(graph, 7, 3), graph.index).plot() 

enter image description here


爲了解決誤解

  1. 的其他一些觀點是pandas.Series一個pandas.DataFrame。 A pandas.DataFrame可以被認爲是pandas.Seriespandas.Series
  2. 因此,您使用graph.indexgraph.values訪問該系列的索引。
  3. 你可能也做

    import matplotlib.pyplot as plt 
    
    plt.plot(graph.index, savgol_filter(graph.values, 7, 3)) 
    

enter image description here

0

由於您使用的是Series而不是DataFrame,某些庫無法訪問索引以將其用作列。
用途:

df = df.reset_index() 

將索引轉換爲可以在savgol過濾器或任何其他使用一個額外的列。