Pandas 如何平滑数据帧中的线条?

Pandas 如何平滑数据帧中的线条?,pandas,dataframe,plot,Pandas,Dataframe,Plot,我有一个小数据帧,其中包含几类插值数据,它们的值在0和1之间进行了标准化。我正试图追踪平滑曲线,就像在EXCEL中一样,但在论坛中阅读其他问题时,这需要进一步插值,我不知道这样做是否正确?如何获得与EXCEL中相同的图形 xl = pd.ExcelFile('C:/.../test.xlsx') df1 = xl.parse(0, skipfooter= nrows-(10),index_col='Classes',header=0).dropna(axis=1, how='all') df1

我有一个小数据帧,其中包含几类插值数据,它们的值在0和1之间进行了标准化。我正试图追踪平滑曲线,就像在EXCEL中一样,但在论坛中阅读其他问题时,这需要进一步插值,我不知道这样做是否正确?如何获得与EXCEL中相同的图形

xl = pd.ExcelFile('C:/.../test.xlsx')
df1 = xl.parse(0, skipfooter= nrows-(10),index_col='Classes',header=0).dropna(axis=1, how='all')
df1

                A           B           C           D           E           F           G           H           I           L
Classes                                     
class 1     0.167205    0.117160    0.293759    0.114839    0.010403    0.009577    0.013579    0.010279    0.295320    0.496107
class 2     1.000000    1.000000    1.000000    1.000000    0.393559    0.301260    0.281466    0.230577    0.198755    0.416739
class 3     0.008582    0.054056    0.007861    0.072378    0.703360    0.817691    0.803952    0.803575    0.000000    0.000000
class 4     0.135236    0.087106    0.255319    0.077556    0.000000    0.000000    0.000000    0.000000    0.252681    0.443720
class 5     0.041120    0.140389    0.033002    0.279836    1.000000    1.000000    1.000000    1.000000    0.615051    0.248261
class 6     0.000000    0.000000    0.000000    0.000000    0.667189    0.796265    0.782126    0.768850    0.078520    0.035903
class 7     0.654654    0.644665    0.740677    0.784618    0.508319    0.427955    0.426612    0.401502    1.000000    1.000000
class 8     0.121820    0.073066    0.268800    0.099552    0.000940    0.003957    0.010434    0.012108    0.352075    0.529671
class 9     0.139109    0.118368    0.215398    0.127073    0.262349    0.270412    0.263293    0.257149    0.266421    0.347188

df1.T.plot(figsize=(8,5))

胜过


您需要对数据进行插值,即添加一些辅助点

df = df1.T.reset_index(drop=True)  # transpose dataframe
df = df.reindex(df.index.union(pd.np.linspace(df.index.min(),df.index.max(), df.index.shape[0]*10))).reset_index(drop=True)  # insert 10 "empty" points between existing ones
df = df.interpolate('pchip', order=2)  # fill the gaps with values
df.plot()  # draw new Dataframe
这规定如下:

插值函数的选择取决于您。还有很多其他功能,但IMO
pchip
在平滑度和精度之间提供了最佳折衷