![](/img/trans.png)
[英]How do you iterate through groups in a pandas Dataframe, operate on each group, then assign values to the original dataframe?
[英]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.