python - 从 pandas 系列中选择列

标签 python pandas series

我在 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

我正在使用 graph.plot() 绘制曲线,如下所示: enter image description here

目标是平滑曲线。我试图使用 Savgol_Filter ,但要做到这一点,我需要将我的系列分成 x 和 y 列。截至目前,我可以使用 graph.index 访问“波长”列,但我无法获取下一列将其指定为 y。

我尝试过使用 iloc 和 loc,但还没有任何运气。

有什么建议或新方向可以尝试吗?

最佳答案

您不需要将 xy 传递给 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()

enter image description here

<小时/>

解决其他一些误解

  1. graph是一个pandas.Series不是一个pandas.DataFramepandas.DataFrame 可以被认为是 pandas.Seriespandas.Series
  2. 因此,您可以使用 graph.index 访问系列索引,并使用 graph.values 访问值。
  3. 你也可以这样做

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

enter image description here

关于python - 从 pandas 系列中选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46732910/

相关文章:

python - 如何在 Django 中表示学生与讲师的关系?

python - 使用 pandas 检查哪一列存在值

python - 如何转换日期中的月份?

python-3.x - Pandas系列垂直合并

python - Django 中的简单线程

python - 列表应该总是转换为元组吗?

python - 在 Pandas 数据框中查找重复行

python - 将 Dataframe 列的内容“扩展”到新列中

python - 使用 pandas dataframe 将值写入新的 csv 文件错误

python - 如何通过构造简单的 pandas Series 来定义正确的索引?