Python Forum
newbie: loop, modify dataframe cells
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
newbie: loop, modify dataframe cells
I really tried to do this myself for hours, and cannot get the hang of it. I simply want to loop through a dataframe, check the value in each cell and optionally modify it. In this case simplicity is more important than efficiency but it would be good to know the most efficient ways of doing it as well.

I can iterate through the dataframe in a number of ways but don't know how to select, inspect and modify the current cell. Also that modification may be based on the value of another cell in the same row so need to know how to find them as well.

Thank you.
Here's a quickie bare bones tutorial:
Thanks but first of all, that link is broken.

Then I've been through many tutorials, both text and video, and while found bits and pieces of what I want to do, none that pull them together. For example, many ways to loop/iterate through the dataframe, but only print the cell contents, not modify it. Or how to modify a specific cell, but not in the context of looping through them - only by selecting a specific one with .loc or another method. So basic introductions are not helpful to me.
What is wrong with simple .apply? Something like:

>>> df[“colname”] = df[“colname”].apply(do_your_magic_here)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
corrected link:
This creates a dataframe, loops through and prints the values, and if the value is 6 it is modified to 100.
import pandas as pd
df = pd.DataFrame(({1,2,3},{4,5,6},{7,8,9}))
for row in range(3) :
    for col in range(3) :
        if df.iloc[row,col] == 6 :
            df.iloc[row,col] = 100

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interate for loop over certain columns in dataframe Finpyth 2 535 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  How modify the DataFrame columns SriRajesh 2 696 Sep-12-2019, 03:14 PM
Last Post: SriRajesh
  Double 'for' loop and writing in a new columns dataframe marco_ita 0 571 Sep-07-2019, 12:44 PM
Last Post: marco_ita
  compare and modify columns in dataframe DionisiO 1 731 Feb-23-2019, 11:07 PM
Last Post: tiredAcademic
  Python QtableWidget get text of all cells and headers to dataframe Mady 3 9,848 Dec-15-2018, 06:46 PM
Last Post: Axel_Erfurt
  Converting days to years in loop while computing values across grid cells Lightning1800 2 1,164 May-15-2018, 08:44 PM
Last Post: Lightning1800
  build pandas dataframe from a for loop vaison 4 52,613 Apr-14-2018, 04:34 PM
Last Post: vaison
  Newbie question to return only the index of a dataframe zydjohn 0 1,117 Jan-22-2018, 03:40 PM
Last Post: zydjohn
  Newbie question: how to generate dataframe and use multiple regression zydjohn 0 1,061 Dec-10-2017, 09:49 AM
Last Post: zydjohn

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020