Python: Learn Pandas Query and Eval ver. 1

Last Updated on September 7, 2022 by shibatau

I. What shall we learn?

We will learn how to use Pandas Query and Eval from the post:

Level up your Pandas skills with query() and eval()

II. Basic Use

1. Creating a data frame

import pandas as pd
import numpy as np

data = []
for _ in range(100):
    data.append({
        "gender": "Male",
        "height": np.random.normal(178, 10),
        "age": np.random.uniform(20, 70)
    })
for _ in range(100):
    data.append({
        "gender": "Female",
        "height": np.random.normal(166, 8),
        "age": np.random.uniform(20, 70)
    })
df = (pd.DataFrame(data)
    # sample to mix order
    .sample(frac=1.0, replace=False)
    .reset_index(drop=True)
 )

II. Using query and eval

Let’s extract the females between age 20–30 and calculate the tallest and shortest among them using query and eval.

You can see the scripts on Google Colaboratory:

https://colab.research.google.com/drive/1bYFgOp1EyUcjIHYb5dYJ30V9vI7ub2OI?usp=sharing

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.