Subset by condition

Python Practice › Pandas Programs › Program 16

Subset by condition

EasyPandas Programs · 16 of 95

Write a pandas program to select the values below 40.

Input
[10, 25, 69, 74, 33, 54, 21]
Output
0    10
1    25
4    33
6    21
dtype: int64

Approach

  1. Boolean indexing.

Solution

Python
import pandas as pds = pd.Series([10, 25, 69, 74, 33, 54, 21])print(s[s < 40])
Output
0    10
1    25
4    33
6    21
dtype: int64

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Add data to a Series

Python Practice › Pandas Programs › Program 15

Add data to a Series

EasyPandas Programs · 15 of 95

Write a pandas program to add new values to an existing Series.

Input
["54", "27.90", "sqa", "33.33", "tools"] + ["python", "100"]
Output
0        54
1     27.90
2       sqa
3     33.33
4     tools
5    python
6       100
dtype: str

Approach

  1. pd.concat with a new Series.

Solution

Python
import pandas as pds = pd.Series(["54", "27.90", "sqa", "33.33", "tools"])s = pd.concat([s, pd.Series(["python", "100"])], ignore_index=True)print(s)
Output
0        54
1     27.90
2       sqa
3     33.33
4     tools
5    python
6       100
dtype: str

Line by line

Line 3

ignore_index=True renumbers the result 0 to 6.

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Sort a Series

Python Practice › Pandas Programs › Program 14

Sort a Series

EasyPandas Programs · 14 of 95

Write a pandas program to sort a Series.

Input
[55, 23, 10, 87]
Output
2    10
1    23
0    55
3    87
dtype: int64

Approach

  1. sort_values().

Solution

Python
import pandas as pdprint(pd.Series([55, 23, 10, 87]).sort_values())
Output
2    10
1    23
0    55
3    87
dtype: int64

Line by line

Line 2

The index labels move with their values.

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Series of lists to one Series

Python Practice › Pandas Programs › Program 13

Series of lists to one Series

EasyPandas Programs · 13 of 95

Write a pandas program to turn a Series of lists into one flat Series.

Input
[["Sqa", "tool"], ["Learning", "python"], ["Is", "fun"]]
Output
0         Sqa
1        tool
2    Learning
3      python
4          Is
5         fun
dtype: str

Approach

  1. explode() puts each list item on its own row.

Solution

Python
import pandas as pds = pd.Series([["Sqa", "tool"], ["Learning", "python"], ["Is", "fun"]])print(s.explode().reset_index(drop=True))
Output
0         Sqa
1        tool
2    Learning
3      python
4          Is
5         fun
dtype: str

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Series to array

Python Practice › Pandas Programs › Program 12

Series to array

EasyPandas Programs · 12 of 95

Write a pandas program to convert a Series to a NumPy array.

Input
["54", "27.90", "sqa", "33.33", "tools"]
Output
['54' '27.90' 'sqa' '33.33' 'tools']

Approach

  1. to_numpy().

Solution

Python
import pandas as pdprint(pd.Series(["54", "27.90", "sqa", "33.33", "tools"]).to_numpy())
Output
['54' '27.90' 'sqa' '33.33' 'tools']

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Convert a Series to numbers

Python Practice › Pandas Programs › Program 11

Convert a Series to numbers

EasyPandas Programs · 11 of 95

Write a pandas program to convert a mixed Series to numbers, turning text into NaN.

Input
["54", "27.90", "sqa", "33.33", "tools"]
Output
0    54.00
1    27.90
2      NaN
3    33.33
4      NaN
dtype: float64

Approach

  1. pd.to_numeric(..., errors="coerce").

Solution

Python
import pandas as pds = pd.Series(["54", "27.90", "sqa", "33.33", "tools"])print(pd.to_numeric(s, errors="coerce"))
Output
0    54.00
1    27.90
2      NaN
3    33.33
4      NaN
dtype: float64

Line by line

Line 3

errors="coerce" turns values that are not numbers into NaN instead of raising an error.

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

NumPy array to Series

Python Practice › Pandas Programs › Program 10

NumPy array to Series

EasyPandas Programs · 10 of 95

Write a pandas program to convert a NumPy array to a Series.

Input
[5, 3, 8, 9, 0]
Output
0    5
1    3
2    8
3    9
4    0
dtype: int64

Approach

  1. pd.Series(array).

Solution

Python
import numpy as npimport pandas as pdprint(pd.Series(np.array([5, 3, 8, 9, 0])))
Output
0    5
1    3
2    8
3    9
4    0
dtype: int64

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Dictionary to Series

Dictionary to Series

EasyPandas Programs · 9 of 95

Write a pandas program to convert a dictionary to a Series.

Input
{"name": "Virat", "sport": "cricket", "age": 32}
Output
name       Virat
sport    cricket
age           32
dtype: object

Approach

  1. Keys become the index.

Solution

Python
import pandas as pdprint(pd.Series({"name": "Virat", "sport": "cricket", "age": 32}))
Output
name       Virat
sport    cricket
age           32
dtype: object

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Is one Series greater?

Is one Series greater?

EasyPandas Programs · 8 of 95

Write a pandas program to check element-wise whether one Series is greater than another.

Input
[1, 6, 9, 5], [5, 2, 4, 5]
Output
[False, True, True, False]

Approach

  1. >.

Solution

Python
import pandas as pda, b = pd.Series([1, 6, 9, 5]), pd.Series([5, 2, 4, 5])print((a > b).tolist())
Output
[False, True, True, False]

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Are Series elements equal?

Are Series elements equal?

EasyPandas Programs · 7 of 95

Write a pandas program to compare two Series element by element.

Input
[1, 6, 9, 5], [5, 2, 4, 5]
Output
[False, False, False, True]

Approach

  1. ==.

Solution

Python
import pandas as pda, b = pd.Series([1, 6, 9, 5]), pd.Series([5, 2, 4, 5])print((a == b).tolist())
Output
[False, False, False, True]

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs