我在 pandas 中有一个名为“graph”的系列,如下所示:
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
目标是平滑曲线。我试图使用 Savgol_Filter ,但要做到这一点,我需要将我的系列分成 x 和 y 列。截至目前,我可以使用 graph.index 访问“波长”列,但我无法获取下一列将其指定为 y。
我尝试过使用 iloc 和 loc,但还没有任何运气。
有什么建议或新方向可以尝试吗?
最佳答案
您不需要将 x
和 y
传递给 savgol_filter
。您只需要在将 graph
传递给它时自动传递的 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()
<小时/>
解决其他一些误解
graph
是一个pandas.Series
,不是一个pandas.DataFrame
。pandas.DataFrame
可以被认为是pandas.Series
的pandas.Series
。- 因此,您可以使用
graph.index
访问系列索引,并使用graph.values
访问值。 你也可以这样做
import matplotlib.pyplot as plt plt.plot(graph.index, savgol_filter(graph.values, 7, 3))
关于python - 从 pandas 系列中选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46732910/