Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Getting Familiar With lambda Functions | Getting Familiar With Indexing and Selecting Data
Advanced Techniques in pandas
course content

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

bookGetting Familiar With lambda Functions

Sometimes we need to put some conditions on the indices. In these cases, you need to use a lambda function inside iloc[].

Let's figure out what we can do using lambda:

This code will output the first five rows of the dataset, the rows with the indices 0, 1, 2, 3, and 4.

  • lambda x - x is the argument we will work with (the item of the data set);
  • x.index - extracts only values of rows' indices;
  • x.index < 5 - the condition according to which we will extract data. Here, only rows with indices that are less than 5.
Task
test

Swipe to show code editor

Your task here is to divide data into two groups: one has odd indices and the other even. Follow the algorithm:

  1. Import the pandas library with the pd alias.
  2. Read the csv file.
  3. Extract only rows with even indices:
    • Apply the .iloc[] attribute to the data;
    • Within the .iloc[] attribute, apply the lambda function with the x argument;
    • Set a condition to check if the number is even (if you do not know how to do this, check the hint).
  4. Extract only rows with odd indices:
    • Apply the .iloc[] attribute to the data;
    • Within the .iloc[] attribute, apply the lambda function with the x argument;
    • Set a condition to check if the number is odd (if you do not know how to do this, check the hint).
  5. Output data:
    • Output the first five rows of the even indices;
    • Output the last five rows of the odd indices.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
toggle bottom row

bookGetting Familiar With lambda Functions

Sometimes we need to put some conditions on the indices. In these cases, you need to use a lambda function inside iloc[].

Let's figure out what we can do using lambda:

This code will output the first five rows of the dataset, the rows with the indices 0, 1, 2, 3, and 4.

  • lambda x - x is the argument we will work with (the item of the data set);
  • x.index - extracts only values of rows' indices;
  • x.index < 5 - the condition according to which we will extract data. Here, only rows with indices that are less than 5.
Task
test

Swipe to show code editor

Your task here is to divide data into two groups: one has odd indices and the other even. Follow the algorithm:

  1. Import the pandas library with the pd alias.
  2. Read the csv file.
  3. Extract only rows with even indices:
    • Apply the .iloc[] attribute to the data;
    • Within the .iloc[] attribute, apply the lambda function with the x argument;
    • Set a condition to check if the number is even (if you do not know how to do this, check the hint).
  4. Extract only rows with odd indices:
    • Apply the .iloc[] attribute to the data;
    • Within the .iloc[] attribute, apply the lambda function with the x argument;
    • Set a condition to check if the number is odd (if you do not know how to do this, check the hint).
  5. Output data:
    • Output the first five rows of the even indices;
    • Output the last five rows of the odd indices.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
toggle bottom row

bookGetting Familiar With lambda Functions

Sometimes we need to put some conditions on the indices. In these cases, you need to use a lambda function inside iloc[].

Let's figure out what we can do using lambda:

This code will output the first five rows of the dataset, the rows with the indices 0, 1, 2, 3, and 4.

  • lambda x - x is the argument we will work with (the item of the data set);
  • x.index - extracts only values of rows' indices;
  • x.index < 5 - the condition according to which we will extract data. Here, only rows with indices that are less than 5.
Task
test

Swipe to show code editor

Your task here is to divide data into two groups: one has odd indices and the other even. Follow the algorithm:

  1. Import the pandas library with the pd alias.
  2. Read the csv file.
  3. Extract only rows with even indices:
    • Apply the .iloc[] attribute to the data;
    • Within the .iloc[] attribute, apply the lambda function with the x argument;
    • Set a condition to check if the number is even (if you do not know how to do this, check the hint).
  4. Extract only rows with odd indices:
    • Apply the .iloc[] attribute to the data;
    • Within the .iloc[] attribute, apply the lambda function with the x argument;
    • Set a condition to check if the number is odd (if you do not know how to do this, check the hint).
  5. Output data:
    • Output the first five rows of the even indices;
    • Output the last five rows of the odd indices.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Sometimes we need to put some conditions on the indices. In these cases, you need to use a lambda function inside iloc[].

Let's figure out what we can do using lambda:

This code will output the first five rows of the dataset, the rows with the indices 0, 1, 2, 3, and 4.

  • lambda x - x is the argument we will work with (the item of the data set);
  • x.index - extracts only values of rows' indices;
  • x.index < 5 - the condition according to which we will extract data. Here, only rows with indices that are less than 5.
Task
test

Swipe to show code editor

Your task here is to divide data into two groups: one has odd indices and the other even. Follow the algorithm:

  1. Import the pandas library with the pd alias.
  2. Read the csv file.
  3. Extract only rows with even indices:
    • Apply the .iloc[] attribute to the data;
    • Within the .iloc[] attribute, apply the lambda function with the x argument;
    • Set a condition to check if the number is even (if you do not know how to do this, check the hint).
  4. Extract only rows with odd indices:
    • Apply the .iloc[] attribute to the data;
    • Within the .iloc[] attribute, apply the lambda function with the x argument;
    • Set a condition to check if the number is odd (if you do not know how to do this, check the hint).
  5. Output data:
    • Output the first five rows of the even indices;
    • Output the last five rows of the odd indices.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt