2017-10-13 71 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过滤器或任何其他使用一个额外的列。