簡體   English   中英

如何遍歷熊貓數據框中的每個列和每個單元格

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

我有一個數據幀( 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以及數據挖掘來說,我是一個新手,所以如果有人可以澄清一下,我將不勝感激。 提前致謝。

我將要做的 ..

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM