简体   繁体   中英

how to iterate through each columns and each cells in a pandas dataframe

I have a dataframe ( training_df ) with 4 columns each containing about 150 rows. I also have function as follows:

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

Now, what I want to do is to iterate through all four columns of my dataframe in turn and iterate through all the rows in each column and for each value in the rows (there will of course be only one cell in each row) I want to replace them with whatever value is returned from the normalise function. So I tried something like this by looking at the already asked questions in this forum:

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)

But I get a TypeError: string indices must be integers I am quite new to Python and pandas and to data mining, so if somebody could clarify this a bit would really appreciate it. Thanks in advance.

我将要做的 ..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM