Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Removing Rows | Preprocessing Data: Part II
Data Manipulation using pandas
course content

Course Content

Data Manipulation using pandas

Data Manipulation using pandas

1. Preprocessing Data: Part I
2. Preprocessing Data: Part II
3. Grouping Data
4. Aggregating and Visualizing Data
5. Joining Data

bookRemoving Rows

Let's see what are the differences that caused these issues by displaying these rows.

12345678
# Importing the library import pandas as pd # Reading the file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data2.csv') # Select rows with discrepancies ind = df.iloc[:,2:15].sum(axis = 1) != df.hhsize print(df.loc[ind, df.columns[1:15]])
copy

These are not consequent observations, and it's only half a percent of all observations, so we can easily delete them. If you want to delete rows or columns, apply the .drop() method to dataframe. If you want to remain changes saved, either reassign to dataframe result of applying method, or set the inplace = True parameter. If you want to drop rows, set the index parameter to indexes of rows you want to remove, if you want to delete columns - set the columns parameter to list of columns you want to delete. For instance, if you want to delete the first, and the third rows, you should apply the .drop(index = [0, 2]) method. If you want to delete 3-5 columns, then you can get their names from the .columns attribute, and apply the .drop(columns = df.columns[2:5]) method. Feel free to experiment!

12345678910
# Importing the library import pandas as pd # Reading the file df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data2.csv') # Dropping rows print(dr.drop(index = [0, 2])) # Dropping columns print(df.drop(columns = df.columns[2:5]))
copy

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
some-alt