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
Iterating through pandas dataFrame objects is generally slow. Pandas Iteration beats the whole purpose of using DataFrame. It is an anti-pattern and is something you should only do when you have exhausted every other option. It is better look for a List Comprehensions , vectorized solution or DataFrame.apply() method.

Pandas DataFrame loop using list comprehension

result = [(x, y,z) for x, y,z in zip(df['Name'], df['Promoted'],df['Grade'])]

Pandas DataFrame loop using DataFrame.apply()

result = df.apply(lambda row: row["Name"] + " , " + str(row["TotalMarks"]) + " , " + row["Grade"], axis = 1)
Reply
#4
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
Lightbulb [Solved] df.loc: write data in certain rows ju21878436312 1 431 Jun-28-2021, 06:49 AM
Last Post: ju21878436312
  Filtering a data frame according to number of occurences of an observation Menthix 1 482 May-31-2021, 10:50 PM
Last Post: supuflounder
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 554 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 1,970 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Pandas: how to split one row of data to multiple rows and columns in Python GerardMoussendo 4 1,927 Feb-22-2021, 06:51 PM
Last Post: eddywinch82
  How to filter out Column data From Multiple rows data? firaki12345 10 1,408 Feb-06-2021, 04:54 AM
Last Post: buran
  get year information from a timestamp data frame asli 1 479 Jan-08-2021, 09:11 PM
Last Post: Larz60+
  update column in one data frame with value of column from another data frame flexer 0 580 Dec-04-2020, 03:29 PM
Last Post: flexer
  Moving Rows From Different Data Frames JoeDainton123 1 1,288 Aug-06-2020, 05:19 AM
Last Post: scidam
  Extracting Rows From Data Frame and Understanding The Code JoeDainton123 0 603 Aug-03-2020, 04:08 PM
Last Post: JoeDainton123

Forum Jump:

User Panel Messages

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