首页 > 解决方案 > Python:数据框列表中的标准偏差

问题描述

我有一个说 50 个数据帧“list1”的列表,每个数据帧都有“速度”和“值”列,就像这样;

Speed   Value
1       12
2       17
3       19
4       21
5       25

我正在尝试获取所有数据帧中每种速度的“值”的标准偏差。最终目标是获得每个速度的标准偏差列表或 df,如下所示;

Speed   Standard Deviation
1       1.23
2       2.5
3       1.98
4       5.6
5       5.77

我尝试使用 for 循环将值拉入一个新的数据帧,然后使用“statistics.stdev”,但我似乎无法让它工作。任何帮助真的很感激!

更新!

pd.concat([d.set_index('Speed').values for d in df_power], axis=1).std(1)

这行得通。虽然,我忘了提到速度的值在数据帧之间并不总是相同的。一些数据框会丢失一些数据框,最终会在这些情况下返回 nan 。

标签: pythonpandasstatistics

解决方案


您可以concat使用std

list_df = [df1, df2, df3, ...]
pd.concat([d.set_index('Speed') for d in list_dfs], axis=1).std(1)

推荐阅读