Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何遍历数据帧中的每个列和每个单元格_Python_Pandas_Dataframe_Classification_Data Mining - Fatal编程技术网

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

Python 如何遍历数据帧中的每个列和每个单元格,python,pandas,dataframe,classification,data-mining,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) 现在,我要做的是依次遍历数据帧的所有四列,遍历

我有一个数据帧(
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)
但是我得到了一个
类型错误:字符串索引必须是整数
我对Python和pandas以及数据挖掘非常陌生,所以如果有人能澄清一下这一点,我会非常感激。提前谢谢。

我要做什么

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

df=df-df.min()/(df.max()-df.min())
同样有效谢谢您的回复@Wen。问题不在于函数,而是每次测试时都返回所需的值。当我尝试替换数据帧中的值时,就会出现问题。以下部分工作正常:对于training_df中的列:theMin=training_df[column].min()theMax=training_df[column].max(),但当我尝试用返回值替换每个单元格中的值时,它会给我一个error@SedatTurkoglu在training_df[[column]]中将x,i更改为
。iterrows():training_df.loc[x,column]=正常化(theMin,theMax,i[0])
Awesome@Wen,这正是我需要的,非常感谢:)