Python Forum
Iterating Through Data Frame Rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterating Through Data Frame Rows
#1
Hello all

I have a dataframe which i have imported from excel.

It has column headings and below the column headings it has all of my data in rows.

I want to LOOP through each row of the first column and if it is equal to a value say 123 then i just want to remove it.

I want to LOOP through each item in the data frame.

so far i have:-

t = 0
while t < len(Report):
Value = Report.iloc[t,0] #the value i want to check is always in the 0 column index
if Value == Report.iloc[t,0]:

#how do i delete the the row????

t=t+1
deleting the rows is proving to be very difficult.

Can anyone help provide some guidance.

Thank you.
Reply
#2
Usually, you don't need to use pure Python loops to drop rows;

df[~(df.iloc[:,0] == value)]  # remove row if a specific value in the first column;
or you can use .drop method

df.drop(df[df.iloc[:,0] == value].index, inplace=True)
Reply
#3
Not being a scientist, I don't have any large amounts of experimental data to process, but pandas is interesting!

Have a look here perhaps!

import pandas as pd

# open the file
xl_file = '/home/pedro/myPython/pandas/delete_row.xlsx'
df = pd.read_excel(xl_file)

"""
>>> df
   col1  col2  col3  col4  col5  col6
0   123     1     2     3     4     5
1   123     2     3     4     5     6
2   456     3     4     5     6     7
3   456     4     5     6     7     8
4   789     5     6     7     8     9
5   789     6     7     8     9    10
6   123     7     8     9    10    11

"""

# Drop a row by condition

df2 = df[df.col1 != 123]

"""
>>> df2
   col1  col2  col3  col4  col5  col6
2   456     3     4     5     6     7
3   456     4     5     6     7     8
4   789     5     6     7     8     9
5   789     6     7     8     9    10
>>>
"""
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how do you style data frame that has empty rows. gsaray101 0 499 Sep-08-2023, 05:20 PM
Last Post: gsaray101
  googletrans library to translate text language for using data frame is not running gcozba2023 0 1,164 Mar-06-2023, 09:50 AM
Last Post: gcozba2023
  (Python) Pulling data from UA Google Analytics with more than 100k rows into csv. Stockers 0 1,172 Dec-19-2022, 11:11 PM
Last Post: Stockers
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,802 Dec-12-2022, 08:22 PM
Last Post: jh67
  Load multiple Jason data in one Data Frame vijays3 6 1,500 Aug-12-2022, 05:17 PM
Last Post: vijays3
  conditionals based on data frame mbrown009 1 873 Aug-12-2022, 08:18 AM
Last Post: Larz60+
  Merging two Data Frame on a special case piku9290dgp 0 1,069 Mar-02-2022, 10:43 AM
Last Post: piku9290dgp
  Save data frame to .csv df.to.csv() mcva 1 1,497 Feb-03-2022, 07:05 PM
Last Post: mcva
  Move a particular row in pandas data frame to last row klllmmm 0 3,647 Dec-27-2021, 09:11 AM
Last Post: klllmmm
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,602 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983

Forum Jump:

User Panel Messages

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