我有一个数据框 (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/