Python Forum
newbie: loop, modify dataframe cells
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
newbie: loop, modify dataframe cells
#6
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) :
        print(df.iloc[row,col])
        if df.iloc[row,col] == 6 :
            df.iloc[row,col] = 100
df
Reply


Messages In This Thread
newbie: loop, modify dataframe cells - by expat_th - Mar-03-2020, 05:45 AM
RE: newbie: loop, modify dataframe cells - by jefsummers - Mar-03-2020, 09:05 PM

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

Forum Jump:

User Panel Messages

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