![](/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.