【问题标题】:Problem with different column lengths in Pandas DataframePandas Dataframe 中不同列长度的问题
【发布时间】:2022-08-05 22:43:17
【问题描述】:

我知道如何解决它可能很明显,但我没有想法......

我将带有 Pandas 的 .csv 文件导入数据框。数据具有以下格式: 3 列单标题,第 1 列:45 行,第 2 列 40 行,第 3 列:21 行。 然后形状是 (45,3)。 \"missing\" 行充满了 NAN,我的问题从这里开始。

我想评估一些具有不同 scipy 函数的统计数据,例如 Anderson Darling 测试等,如下所示:

for i in columns:
print ([i])
a = stats.anderson(df[i], dist = \'norm\')
print (a)
if a[0] > a[1][2]:
    print(\'The null hypothesis can be rejected at\', a[2][2],\'% significance level\')
else:
    print(\'The null hypothesis cannot be rejected\')

所以,第一列得到评估就好了:

[\'Z79V0001\']AndersonResult(statistic=0.41768739435435975, critical_values=array([0.535, 0.609, 0.731, 0.853, 1.014]), significance_level=array([15. , 10. ,  5. ,  2.5,  1. ]))The null hypothesis cannot be rejected

但对于其他人,我得到类似的东西

[\'Z79V0003_1\']AndersonResult(statistic=nan, critical_values=array([0.535, 0.609, 0.731, 0.853, 1.014]), significance_level=array([15. , 10. ,  5. ,  2.5,  1. ]))

不能拒绝原假设 用零填充 NAN 值无济于事,因为随后将计算统计信息 错误的方法。我根本无法解决如何调整列的长度,以便函数只在它找到数字的行上工作,如果到达 NAN 继续下一列...... 帮助将不胜感激。

  • a = stats.anderson(df[i].dropna().values, dist = \'norm\')

标签: python pandas dataframe scipy


【解决方案1】:

如果您将 numpy 数组传递给 stats 函数,这将是最简单的。您可以使用每列的 Series 方法删除 NaN:

for col in df.columns:
    a = stats.anderson(df[col].dropna().values, dist = 'norm') 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-04
    • 2021-03-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2020-04-17
    • 1970-01-01
    相关资源
    最近更新 更多