You are on page 1of 6

7/8/22, 12:22 PM Daily Task 3 & 4 - query function & list comprehension_06-07-2022 - Jupyter Notebook

Daily Task 3 - Query Function of data_frame

Example - 1

In [1]:

import pandas as pd

In [2]:

info = { "Name" : ['Pooja','Vrushali','Karan','Mona','Divya','Yogesh'],


"Age" : [28,29,30,25,27,29],
"Company" : ['Govt','Infy','L&T','Google','Sports','IBM'],
"Designation" : ['IPS','Data Scientist','Senior Engineer','Trainee','Caption','HR
"Salary" : [120000,90000,80000,350000,150000,60000]
}

employees = pd.DataFrame(info)
print(employees)

Name Age Company Designation Salary

0 Pooja 28 Govt IPS 120000

1 Vrushali 29 Infy Data Scientist 90000

2 Karan 30 L&T Senior Engineer 80000

3 Mona 25 Google Trainee 350000

4 Divya 27 Sports Caption 150000

5 Yogesh 29 IBM HR Manager 60000

In [3]:

employees.query('Salary > 80000')

Out[3]:

Name Age Company Designation Salary

0 Pooja 28 Govt IPS 120000

1 Vrushali 29 Infy Data Scientist 90000

3 Mona 25 Google Trainee 350000

4 Divya 27 Sports Caption 150000

In [4]:

employees.query('Age == 29')

Out[4]:

Name Age Company Designation Salary

1 Vrushali 29 Infy Data Scientist 90000

5 Yogesh 29 IBM HR Manager 60000

Example -2
localhost:8888/notebooks/Python by John/Daily Tasks/Daily Task 3 %26 4 - query function %26 list comprehension_06-07-2022.ipynb 1/6
7/8/22, 12:22 PM Daily Task 3 & 4 - query function & list comprehension_06-07-2022 - Jupyter Notebook

In [10]:

insurance_data = pd.read_csv('E:\Data Science by John\pandas\insurance.csv')


insurance_data

Out[10]:

age sex bmi children smoker region charges

0 19 female 27.900 0 yes southwest 16884.92400

1 18 male 33.770 1 no southeast 1725.55230

2 28 male 33.000 3 no southeast 4449.46200

3 33 male 22.705 0 no northwest 21984.47061

4 32 male 28.880 0 no northwest 3866.85520

... ... ... ... ... ... ... ...

1333 50 male 30.970 3 no northwest 10600.54830

1334 18 female 31.920 0 no northeast 2205.98080

1335 18 female 36.850 0 no southeast 1629.83350

1336 21 female 25.800 0 no southwest 2007.94500

1337 61 female 29.070 0 yes northwest 29141.36030

1338 rows × 7 columns

In [12]:

insurance_data.head(10)

Out[12]:

age sex bmi children smoker region charges

0 19 female 27.900 0 yes southwest 16884.92400

1 18 male 33.770 1 no southeast 1725.55230

2 28 male 33.000 3 no southeast 4449.46200

3 33 male 22.705 0 no northwest 21984.47061

4 32 male 28.880 0 no northwest 3866.85520

5 31 female 25.740 0 no southeast 3756.62160

6 46 female 33.440 1 no southeast 8240.58960

7 37 female 27.740 3 no northwest 7281.50560

8 37 male 29.830 2 no northeast 6406.41070

9 60 female 25.840 0 no northwest 28923.13692

localhost:8888/notebooks/Python by John/Daily Tasks/Daily Task 3 %26 4 - query function %26 list comprehension_06-07-2022.ipynb 2/6
7/8/22, 12:22 PM Daily Task 3 & 4 - query function & list comprehension_06-07-2022 - Jupyter Notebook

In [14]:

insurance_data.query('age > 30')

Out[14]:

age sex bmi children smoker region charges

3 33 male 22.705 0 no northwest 21984.47061

4 32 male 28.880 0 no northwest 3866.85520

5 31 female 25.740 0 no southeast 3756.62160

6 46 female 33.440 1 no southeast 8240.58960

7 37 female 27.740 3 no northwest 7281.50560

... ... ... ... ... ... ... ...

1329 52 male 38.600 2 no southwest 10325.20600

1330 57 female 25.740 2 no southeast 12629.16560

1332 52 female 44.700 3 no southwest 11411.68500

1333 50 male 30.970 3 no northwest 10600.54830

1337 61 female 29.070 0 yes northwest 29141.36030

894 rows × 7 columns

In [16]:

insurance_data.query('children == 2')

Out[16]:

age sex bmi children smoker region charges

8 37 male 29.830 2 no northeast 6406.41070

24 37 male 28.025 2 no northwest 6203.90175

27 55 female 32.775 2 no northwest 12268.63225

29 31 male 36.300 2 yes southwest 38711.00000

41 31 female 36.630 2 no southeast 4949.75870

... ... ... ... ... ... ... ...

1319 39 female 26.315 2 no northwest 7201.70085

1323 42 female 40.370 2 yes southeast 43896.37630

1328 23 female 24.225 2 no northeast 22395.74424

1329 52 male 38.600 2 no southwest 10325.20600

1330 57 female 25.740 2 no southeast 12629.16560

240 rows × 7 columns

===================================================================

localhost:8888/notebooks/Python by John/Daily Tasks/Daily Task 3 %26 4 - query function %26 list comprehension_06-07-2022.ipynb 3/6
7/8/22, 12:22 PM Daily Task 3 & 4 - query function & list comprehension_06-07-2022 - Jupyter Notebook

Daily Task 4 - List Comprehension

syntax for list comprehension : [expression for item in list]

e.g [expression for item in list] as [[x for x in fruits]

Example - 1

In [32]:

nums =[]
values = [i for i in range(1,30,3) if i>10]
nums = [values]
nums

Out[32]:

[[13, 16, 19, 22, 25, 28]]

In [33]:

nums.append(31)
nums

Out[33]:

[[13, 16, 19, 22, 25, 28], 31]

Exapmle - 2

In [34]:

fruits = ["apple", "banana", "cherry", "kiwi", "mango"]


req_fruits = [x for x in fruits if "a" in x]

print(req_fruits)

['apple', 'banana', 'mango']

In [43]:

req_fruits_1 = [f for f in fruits if 'n' in f]


req_fruits_1

Out[43]:

['banana', 'mango']

localhost:8888/notebooks/Python by John/Daily Tasks/Daily Task 3 %26 4 - query function %26 list comprehension_06-07-2022.ipynb 4/6
7/8/22, 12:22 PM Daily Task 3 & 4 - query function & list comprehension_06-07-2022 - Jupyter Notebook

In [47]:

years = [2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010]
leap_year = [y for y in years if y%4 ==0]
leap_year

Out[47]:

[2000, 2004, 2008]

In [50]:

[y for y in years if y>=2005]

Out[50]:

[2005, 2006, 2007, 2008, 2009, 2010]

Example - 3

In [51]:

insurance_data

Out[51]:

age sex bmi children smoker region charges

0 19 female 27.900 0 yes southwest 16884.92400

1 18 male 33.770 1 no southeast 1725.55230

2 28 male 33.000 3 no southeast 4449.46200

3 33 male 22.705 0 no northwest 21984.47061

4 32 male 28.880 0 no northwest 3866.85520

... ... ... ... ... ... ... ...

1333 50 male 30.970 3 no northwest 10600.54830

1334 18 female 31.920 0 no northeast 2205.98080

1335 18 female 36.850 0 no southeast 1629.83350

1336 21 female 25.800 0 no southwest 2007.94500

1337 61 female 29.070 0 yes northwest 29141.36030

1338 rows × 7 columns

In [ ]:

### can wu use list comprehension here?

In [53]:

human = [ letter for letter in 'human' ]


print( human)

['h', 'u', 'm', 'a', 'n']

localhost:8888/notebooks/Python by John/Daily Tasks/Daily Task 3 %26 4 - query function %26 list comprehension_06-07-2022.ipynb 5/6
7/8/22, 12:22 PM Daily Task 3 & 4 - query function & list comprehension_06-07-2022 - Jupyter Notebook

In [55]:

nums = [n for n in range(100) if n % 2 == 0 if n % 5 == 0]


print(nums)

[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

In [61]:

numbers = ["Even" if i%2 == 0 else "Odd" for i in range(10)]


numbers

Out[61]:

['Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd', 'Even', 'Odd']

localhost:8888/notebooks/Python by John/Daily Tasks/Daily Task 3 %26 4 - query function %26 list comprehension_06-07-2022.ipynb 6/6

You might also like