Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Finding the Smallest Values of a Column | Extracting Data
Advanced Techniques in pandas
course content

Зміст курсу

Advanced Techniques in pandas

Advanced Techniques in pandas

1. Getting Familiar With Indexing and Selecting Data
2. Dealing With Conditions
3. Extracting Data
4. Aggregating Data
5. Preprocessing Data

Finding the Smallest Values of a Column

We will learn another crucial function, which outputs the top smallest or largest values. You already know that we can sort values and then extract a specific number of rows. Unsurprisingly, pandas can do so using only one line of code. Look at the example of how to retrieve the oldest fifteen cars:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) data_smallest = data.nsmallest(15, 'Year') print(data_smallest.head(15))

If you want to sort by one column and then by another, just put a list with column names in the necessary order. Look at the example where we will sort firstly by 'Year' and then by 'Engine_volume'. This code will first extract the 5 oldest cars, and then if the years match, the car with the lesser value of the 'Engine_volume' column will take priority:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) data_smallest = data.nsmallest(5, ['Year', 'Engine_volume']) print(data_smallest.head())

Try to compare the two examples below. Now we will advance the function a little bit. Let's return our examples with the column's 'Year' values. In our column, the 'Year' values can be repeated, so if we want to output the ten oldest cars with the previous syntax, our function will take just ten values. It doesn't care if the 11th or 12th value is the same as the 10th. We can put the argument keep = 'all' into the .nsmallest() method to prevent such cases. Look at the example, and try to execute it to see the difference:

1234567891011
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) # Case without using `keep = 'all'` argument data_smallest = data.nsmallest(6, 'Year') print(data_smallest) data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) # Case with using `keep = 'all'` argument data_smallest = data.nsmallest(6, 'Year', keep = 'all') print(data_smallest)

Завдання

Finally, it's time to practice! Here, you should follow this algorithm:

  1. Retrieve data on cars where the column 'Year' values are greater than 2010.
  2. Extract the cheapest 15 cars (the 15 smallest values of the column 'Price'). Include all duplicated values of the column 'Price'.
  3. Output all values of the data set data_cheapest.

Завдання

Finally, it's time to practice! Here, you should follow this algorithm:

  1. Retrieve data on cars where the column 'Year' values are greater than 2010.
  2. Extract the cheapest 15 cars (the 15 smallest values of the column 'Price'). Include all duplicated values of the column 'Price'.
  3. Output all values of the data set data_cheapest.

Все було зрозуміло?

Секція 3. Розділ 5
toggle bottom row

Finding the Smallest Values of a Column

We will learn another crucial function, which outputs the top smallest or largest values. You already know that we can sort values and then extract a specific number of rows. Unsurprisingly, pandas can do so using only one line of code. Look at the example of how to retrieve the oldest fifteen cars:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) data_smallest = data.nsmallest(15, 'Year') print(data_smallest.head(15))

If you want to sort by one column and then by another, just put a list with column names in the necessary order. Look at the example where we will sort firstly by 'Year' and then by 'Engine_volume'. This code will first extract the 5 oldest cars, and then if the years match, the car with the lesser value of the 'Engine_volume' column will take priority:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) data_smallest = data.nsmallest(5, ['Year', 'Engine_volume']) print(data_smallest.head())

Try to compare the two examples below. Now we will advance the function a little bit. Let's return our examples with the column's 'Year' values. In our column, the 'Year' values can be repeated, so if we want to output the ten oldest cars with the previous syntax, our function will take just ten values. It doesn't care if the 11th or 12th value is the same as the 10th. We can put the argument keep = 'all' into the .nsmallest() method to prevent such cases. Look at the example, and try to execute it to see the difference:

1234567891011
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) # Case without using `keep = 'all'` argument data_smallest = data.nsmallest(6, 'Year') print(data_smallest) data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) # Case with using `keep = 'all'` argument data_smallest = data.nsmallest(6, 'Year', keep = 'all') print(data_smallest)

Завдання

Finally, it's time to practice! Here, you should follow this algorithm:

  1. Retrieve data on cars where the column 'Year' values are greater than 2010.
  2. Extract the cheapest 15 cars (the 15 smallest values of the column 'Price'). Include all duplicated values of the column 'Price'.
  3. Output all values of the data set data_cheapest.

Завдання

Finally, it's time to practice! Here, you should follow this algorithm:

  1. Retrieve data on cars where the column 'Year' values are greater than 2010.
  2. Extract the cheapest 15 cars (the 15 smallest values of the column 'Price'). Include all duplicated values of the column 'Price'.
  3. Output all values of the data set data_cheapest.

Все було зрозуміло?

Секція 3. Розділ 5
toggle bottom row

Finding the Smallest Values of a Column

We will learn another crucial function, which outputs the top smallest or largest values. You already know that we can sort values and then extract a specific number of rows. Unsurprisingly, pandas can do so using only one line of code. Look at the example of how to retrieve the oldest fifteen cars:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) data_smallest = data.nsmallest(15, 'Year') print(data_smallest.head(15))

If you want to sort by one column and then by another, just put a list with column names in the necessary order. Look at the example where we will sort firstly by 'Year' and then by 'Engine_volume'. This code will first extract the 5 oldest cars, and then if the years match, the car with the lesser value of the 'Engine_volume' column will take priority:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) data_smallest = data.nsmallest(5, ['Year', 'Engine_volume']) print(data_smallest.head())

Try to compare the two examples below. Now we will advance the function a little bit. Let's return our examples with the column's 'Year' values. In our column, the 'Year' values can be repeated, so if we want to output the ten oldest cars with the previous syntax, our function will take just ten values. It doesn't care if the 11th or 12th value is the same as the 10th. We can put the argument keep = 'all' into the .nsmallest() method to prevent such cases. Look at the example, and try to execute it to see the difference:

1234567891011
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) # Case without using `keep = 'all'` argument data_smallest = data.nsmallest(6, 'Year') print(data_smallest) data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) # Case with using `keep = 'all'` argument data_smallest = data.nsmallest(6, 'Year', keep = 'all') print(data_smallest)

Завдання

Finally, it's time to practice! Here, you should follow this algorithm:

  1. Retrieve data on cars where the column 'Year' values are greater than 2010.
  2. Extract the cheapest 15 cars (the 15 smallest values of the column 'Price'). Include all duplicated values of the column 'Price'.
  3. Output all values of the data set data_cheapest.

Завдання

Finally, it's time to practice! Here, you should follow this algorithm:

  1. Retrieve data on cars where the column 'Year' values are greater than 2010.
  2. Extract the cheapest 15 cars (the 15 smallest values of the column 'Price'). Include all duplicated values of the column 'Price'.
  3. Output all values of the data set data_cheapest.

Все було зрозуміло?

We will learn another crucial function, which outputs the top smallest or largest values. You already know that we can sort values and then extract a specific number of rows. Unsurprisingly, pandas can do so using only one line of code. Look at the example of how to retrieve the oldest fifteen cars:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) data_smallest = data.nsmallest(15, 'Year') print(data_smallest.head(15))

If you want to sort by one column and then by another, just put a list with column names in the necessary order. Look at the example where we will sort firstly by 'Year' and then by 'Engine_volume'. This code will first extract the 5 oldest cars, and then if the years match, the car with the lesser value of the 'Engine_volume' column will take priority:

1234
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) data_smallest = data.nsmallest(5, ['Year', 'Engine_volume']) print(data_smallest.head())

Try to compare the two examples below. Now we will advance the function a little bit. Let's return our examples with the column's 'Year' values. In our column, the 'Year' values can be repeated, so if we want to output the ten oldest cars with the previous syntax, our function will take just ten values. It doesn't care if the 11th or 12th value is the same as the 10th. We can put the argument keep = 'all' into the .nsmallest() method to prevent such cases. Look at the example, and try to execute it to see the difference:

1234567891011
import pandas as pd data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) # Case without using `keep = 'all'` argument data_smallest = data.nsmallest(6, 'Year') print(data_smallest) data = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/4bf24830-59ba-4418-969b-aaf8117d522e/cars.csv', index_col = 0) # Case with using `keep = 'all'` argument data_smallest = data.nsmallest(6, 'Year', keep = 'all') print(data_smallest)

Завдання

Finally, it's time to practice! Here, you should follow this algorithm:

  1. Retrieve data on cars where the column 'Year' values are greater than 2010.
  2. Extract the cheapest 15 cars (the 15 smallest values of the column 'Price'). Include all duplicated values of the column 'Price'.
  3. Output all values of the data set data_cheapest.

Секція 3. Розділ 5
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt