如何遍历 pandas 数据框中的每一列和每个单元格
how to iterate through each columns and each cells in a pandas dataframe
我有一个包含 4 列的数据框 (training_df
),每列包含大约 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()))
我有一个包含 4 列的数据框 (training_df
),每列包含大约 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()))