简体   繁体   English

如何遍历熊猫数据框中的每个列和每个单元格

[英]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.

相关问题 您如何遍历 pandas Dataframe 中的组,对每个组进行操作,然后将值分配给原始 Dataframe? - How do you iterate through groups in a pandas Dataframe, operate on each group, then assign values to the original dataframe? 如何遍历数据框的列,然后在这些列中的每一列上运行 describe() 函数? - How do I iterate through a dataframe's columns and then run the describe() function on each of these columns? 如何遍历 pandas dataframe 列并检查每列的数据类型并在需要时将其更改为另一种数据类型 - How to iterate through a pandas dataframe column and check the datatype of each column and change it another datatype if needed 如何遍历 pandas dataframe 的每一行,然后有条件地在该行中设置一个新值? - How can I iterate through each row of a pandas dataframe, then conditionally set a new value in that row? 如何遍历Pandas数据框行并在每次迭代中创建数据框 - How to iterate over Pandas dataframe row & create a dataframe in each iteration 如何为每个循环遍历数据帧中的两列? - How to for each loop through two columns in a dataframe? 如何为一组 pandas dataframe 正确迭代每一行 - How to properly iterate over each row for a set of pandas dataframe 如何在 pandas dataframe 上迭代以在每次迭代中获取多行 - How iterate on a pandas dataframe to take a number of rows for each iteration 如何遍历 pandas dataframe 中的两列以将值添加到列表中 - How to iterate through two columns in a pandas dataframe to add the values to a list Python - 如何遍历 dataframe 到 append 每一行 - Python - How to iterate through dataframe to append each row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM