代码之家  ›  专栏  ›  技术社区  ›  user7740495

如何遍历pandas数据帧中的每列和每个单元格

  •  3
  • user7740495  · 技术社区  · 6 年前

    我有一个数据框( training_df )有4列,每列包含约150行。我还具有以下功能:

    def normalise(theMin, theMax, theVal):
        if(theMin == theVal):
            return 0
        else if(theMax == theVal):
            return 1
        return (theVal - theMin) / (theMax - theMin)
    

    现在,我想做的是依次遍历我的数据帧的所有四列,遍历每列中的所有行,对于行中的每个值(当然每行中只有一个单元格),我想用从 normalise 作用因此,我通过查看本论坛中已经提出的问题,尝试了类似的方法:

    for column in training_df:
        theMin = training_df[column].min()
        theMax = training_df[column].max()    
        for i in training_df[[column]].iterrows():
            training_df[[column[i]]] = normalise(theMin, theMax, i)
    

    但我有一个 TypeError: string indices must be integers 我对Python和pandas以及数据挖掘非常陌生,所以如果有人能澄清一下这一点,我会非常感激。提前谢谢。

    1 回复  |  直到 6 年前
        1
  •  3
  •   BENY    6 年前

    我将要做的。。

    df.apply(lambda x : (x-x.min())/(x.max()-x.min()))