[英]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. 我有一个数据帧(
training_df
),具有4列,每列包含约150行。 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. 现在,我要做的是依次遍历数据帧的所有四列,并遍历每一列中的所有行以及行中的每个值(当然,每一行中只有一个单元格)用
normalise
函数返回的任何值替换它们。 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. 但是我遇到了
TypeError: string indices must be integers
对于Python和pandas以及数据挖掘来说,我是一个新手,所以如果有人可以澄清一下,我将不胜感激。 Thanks in advance. 提前致谢。
我将要做的 ..
df.apply(lambda x : (x-x.min())/(x.max()-x.min()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.