Dictionary to DataFrame

Python Practice › Pandas Programs › Program 29

Dictionary to DataFrame

EasyPandas Programs · 29 of 95

Write a pandas program to convert a dictionary into a DataFrame.

Output
    name  age
0   Alex   30
1   John   27
2  Peter   29

Approach

  1. Each key becomes a column.

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter"], "age": [30, 27, 29]})print(df)
Output
    name  age
0   Alex   30
1   John   27
2  Peter   29

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Index of smallest and largest

Python Practice › Pandas Programs › Program 28

Index of smallest and largest

EasyPandas Programs · 28 of 95

Write a pandas program to find the index of the smallest and largest values.

Input
[54, 25, 38, 87]
Output
1 3

Approach

  1. idxmin() and idxmax().

Solution

Python
import pandas as pds = pd.Series([54, 25, 38, 87])print(s.idxmin(), s.idxmax())
Output
1 3

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Date strings to datetimes

Python Practice › Pandas Programs › Program 27

Date strings to datetimes

EasyPandas Programs · 27 of 95

Write a pandas program to convert date strings in different formats into datetimes.

Input
["2 Feb 2020", "5/11/2021", "7-8-2022"]
Output
0   2020-02-02
1   2021-11-05
2   2022-08-07
dtype: datetime64[us]

Approach

  1. pd.to_datetime(..., format="mixed", dayfirst=True).

Solution

Python
import pandas as pds = pd.Series(["2 Feb 2020", "5/11/2021", "7-8-2022"])print(pd.to_datetime(s, format="mixed", dayfirst=True))
Output
0   2020-02-02
1   2021-11-05
2   2022-08-07
dtype: datetime64[us]

Line by line

Line 3

format="mixed" parses each value separately; dayfirst=True reads 5/11/2021 as 5 November.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Length of each word

Python Practice › Pandas Programs › Program 26

Length of each word

EasyPandas Programs · 26 of 95

Write a pandas program to count the characters of each word.

Input
["Virat", "Rohit", "Pant", "Shikhar"]
Output
0    5
1    5
2    4
3    7
dtype: int64

Approach

  1. str.len().

Solution

Python
import pandas as pdprint(pd.Series(["Virat", "Rohit", "Pant", "Shikhar"]).str.len())
Output
0    5
1    5
2    4
3    7
dtype: int64
Note

“Virat” and “Rohit” have 5 letters; the original sample showed 6.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Capitalise first and last letters

Python Practice › Pandas Programs › Program 25

Capitalise first and last letters

EasyPandas Programs · 25 of 95

Write a pandas program to make the first and last letter of each word uppercase.

Input
["sqatools", "python", "data", "science"]
Output
0    SqatoolS
1      PythoN
2        DatA
3     SciencE
dtype: str

Approach

  1. map a small function.

Solution

Python
import pandas as pds = pd.Series(["sqatools", "python", "data", "science"])print(s.map(lambda w: w[0].upper() + w[1:-1] + w[-1].upper()))
Output
0    SqatoolS
1      PythoN
2        DatA
3     SciencE
dtype: str

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Items at positions

Python Practice › Pandas Programs › Program 24

Items at positions

EasyPandas Programs · 24 of 95

Write a pandas program to extract items at positions 1, 4 and 7.

Input
[3, 0, 3, 2, 2, 0, 3, 3, 2]
Output
1    0
4    2
7    3
dtype: int64

Approach

  1. iloc with a list.

Solution

Python
import pandas as pds = pd.Series([3, 0, 3, 2, 2, 0, 3, 3, 2])print(s.iloc[[1, 4, 7]])
Output
1    0
4    2
7    3
dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Frequency of each value

Python Practice › Pandas Programs › Program 23

Frequency of each value

EasyPandas Programs · 23 of 95

Write a pandas program to count each value.

Input
[3, 0, 3, 2, 2, 0, 3, 3, 2]
Output
0    2
2    3
3    4
Name: count, dtype: int64

Approach

  1. value_counts().

Solution

Python
import pandas as pdprint(pd.Series([3, 0, 3, 2, 2, 0, 3, 3, 2]).value_counts().sort_index())
Output
0    2
2    3
3    4
Name: count, dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Minimum of a Series

Python Practice › Pandas Programs › Program 22

Minimum of a Series

EasyPandas Programs · 22 of 95

Write a pandas program to find the minimum value.

Input
[54, 38, 67, 87]
Output
38

Approach

  1. min().

Solution

Python
import pandas as pdprint(pd.Series([54, 38, 67, 87]).min())
Output
38

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Maximum of a Series

Python Practice › Pandas Programs › Program 21

Maximum of a Series

EasyPandas Programs · 21 of 95

Write a pandas program to find the maximum value.

Input
[54, 38, 67, 87]
Output
87

Approach

  1. max().

Solution

Python
import pandas as pdprint(pd.Series([54, 38, 67, 87]).max())
Output
87

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Items not in another Series

Python Practice › Pandas Programs › Program 20

Items not in another Series

EasyPandas Programs · 20 of 95

Write a pandas program to get the items of one Series not present in another.

Input
[4, 7, 3], [9, 4, 3]
Output
1    7
dtype: int64

Approach

  1. ~a.isin(b).

Solution

Python
import pandas as pda, b = pd.Series([4, 7, 3]), pd.Series([9, 4, 3])print(a[~a.isin(b)])
Output
1    7
dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs