Iterating Through Data Frame Rows - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Iterating Through Data Frame Rows (/thread-28796.html) |
Iterating Through Data Frame Rows - JoeDainton123 - Aug-03-2020 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+1deleting the rows is proving to be very difficult. Can anyone help provide some guidance. Thank you. RE: Iterating Through Data Frame Rows - scidam - Aug-06-2020 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 methoddf.drop(df[df.iloc[:,0] == value].index, inplace=True) RE: Iterating Through Data Frame Rows - Pedroski55 - Aug-09-2021 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 >>> """ |