python - 如何迭代 pandas 数据框中的每一列和每个单元格

标签 python pandas dataframe classification data-mining

我有一个数据框 (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:字符串索引必须是整数我对Python和pandas以及数据挖掘都很陌生,所以如果有人能澄清这一点,我将非常感激。提前致谢。

最佳答案

我会做什么..

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

关于python - 如何迭代 pandas 数据框中的每一列和每个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49341706/

相关文章:

python - 在 Numpy 中执行 `A[tuple(B.T)]` 的更快方法

python - 如何找到不同值的坐标?

python - 比较两个数组会引发警告。有什么解决方法吗?

python - 如何在给定列值的情况下获取 pandas dataframe 系列名称?

python - 具有基于索引的限制的前向填充列

python - 在 matplotlib 上绘制条形图

python - 从 csv 文件中删除字母和符号 - python 3.7

python - 从 github python 下载和访问数据

python - 从按多列分组的 pandas 数据框中获取嵌套 JSON

python - 来自 Pandas 数据框的多组重复记录