Drop rows with any missing value

Python Practice › Pandas Programs › Program 86

Drop rows with any missing value

EasyPandas Programs · 86 of 95

Write a pandas program to drop rows where at least one value is missing.

Output
    name   age
0   Alex  30.0
2  Peter  29.0

Approach

  1. dropna().

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", np.nan, "Peter", "Klaus"], "age": [30, np.nan, 29, np.nan]})print(df.dropna())
Output
    name   age
0   Alex  30.0
2  Peter  29.0

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Missing values per column

Python Practice › Pandas Programs › Program 85

Missing values per column

EasyPandas Programs · 85 of 95

Write a pandas program to count the missing values in each column.

Output
name    1
age     2
dtype: int64

Approach

  1. isna().sum().

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", np.nan, "Peter", "Klaus"], "age": [30, np.nan, 29, np.nan]})print(df.isna().sum())
Output
name    1
age     2
dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Columns with missing values

Python Practice › Pandas Programs › Program 84

Columns with missing values

EasyPandas Programs · 84 of 95

Write a pandas program to find the columns with at least one missing value.

Output
['name', 'age']

Approach

  1. isna().any().

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"sr": [1, 2, 3, 4], "name": ["Alex", np.nan, "Peter", "Klaus"], "age": [30, np.nan, 29, np.nan]})print(df.columns[df.isna().any()].tolist())
Output
['name', 'age']

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Detect missing values

Python Practice › Pandas Programs › Program 83

Detect missing values

EasyPandas Programs · 83 of 95

Write a pandas program to show where values are missing.

Output
    name    age
0  False  False
1  False   True
2  False  False
3  False   True

Approach

  1. isna().

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, np.nan, 29, np.nan]})print(df.isna())
Output
    name    age
0  False  False
1  False   True
2  False  False
3  False   True

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Merge with different columns

Python Practice › Pandas Programs › Program 82

Merge with different columns

EasyPandas Programs · 82 of 95

Write a pandas program to merge two DataFrames with different columns, keeping all rows.

Output
   Id  name   city
0   1  Asha    NaN
1   2  Ravi   Pune
2   3   NaN  Delhi

Approach

  1. how="outer".

Solution

Python
import pandas as pdd1 = pd.DataFrame({"Id": [1, 2], "name": ["Asha", "Ravi"]})d2 = pd.DataFrame({"Id": [2, 3], "city": ["Pune", "Delhi"]})print(d1.merge(d2, on="Id", how="outer"))
Output
   Id  name   city
0   1  Asha    NaN
1   2  Ravi   Pune
2   3   NaN  Delhi

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Merge on a common column

Python Practice › Pandas Programs › Program 81

Merge on a common column

EasyPandas Programs · 81 of 95

Write a pandas program to join two DataFrames on their common column.

Output
   Id      Name  marks1  marks2
0  S2      Yash      87      70
1  S3  Abhishek      77      65

Approach

  1. merge(on="Id") keeps ids found in both.

Solution

Python
import pandas as pdd1 = pd.DataFrame({"Id": ["S1", "S2", "S3"], "Name": ["Ketan", "Yash", "Abhishek"], "marks1": [90, 87, 77]})d2 = pd.DataFrame({"Id": ["S2", "S3", "S4"], "marks2": [70, 65, 88]})print(d1.merge(d2, on="Id"))
Output
   Id      Name  marks1  marks2
0  S2      Yash      87      70
1  S3  Abhishek      77      65

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Join along columns

Python Practice › Pandas Programs › Program 80

Join along columns

EasyPandas Programs · 80 of 95

Write a pandas program to join two DataFrames side by side.

Output
     Name  Age    City
0    Yash   30    Pune
1  Gaurav   27  Nagpur
2  Sanket   28     NaN

Approach

  1. pd.concat([d1, d2], axis=1); missing rows become NaN.

Solution

Python
import pandas as pdd1 = pd.DataFrame({"Name": ["Yash", "Gaurav", "Sanket"], "Age": [30, 27, 28]})d2 = pd.DataFrame({"City": ["Pune", "Nagpur"]})print(pd.concat([d1, d2], axis=1))
Output
     Name  Age    City
0    Yash   30    Pune
1  Gaurav   27  Nagpur
2  Sanket   28     NaN

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Join along rows

Python Practice › Pandas Programs › Program 79

Join along rows

EasyPandas Programs · 79 of 95

Write a pandas program to join two DataFrames along rows.

Output
   Id     Name  Age
0   1     Yash   30
1   2   Gaurav   27
2   3   Sanket   28
3   4  Atharva   26
4   5   Tanmay   22

Approach

  1. pd.concat([d1, d2], ignore_index=True).

Solution

Python
import pandas as pdd1 = pd.DataFrame({"Id": [1, 2, 3], "Name": ["Yash", "Gaurav", "Sanket"], "Age": [30, 27, 28]})d2 = pd.DataFrame({"Id": [4, 5], "Name": ["Atharva", "Tanmay"], "Age": [26, 22]})print(pd.concat([d1, d2], ignore_index=True))
Output
   Id     Name  Age
0   1     Yash   30
1   2   Gaurav   27
2   3   Sanket   28
3   4  Atharva   26
4   5   Tanmay   22

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Keep only words

Python Practice › Pandas Programs › Program 78

Keep only words

EasyPandas Programs · 78 of 95

Write a pandas program to remove the numbers from an address column and keep the words.

Output
     Name             Address           words
0  Ramesh  297 shukrawar peth  shukrawar peth
1  Suresh    200 ravivar peth    ravivar peth
2  Sanket   090 shanivar peth   shanivar peth

Approach

  1. str.replace(r"\d+", "") then strip.

Solution

Python
import pandas as pddf = pd.DataFrame({"Name": ["Ramesh", "Suresh", "Sanket"],                   "Address": ["297 shukrawar peth", "200 ravivar peth", "090 shanivar peth"]})df["words"] = df["Address"].str.replace(r"\d+", "", regex=True).str.strip()print(df)
Output
     Name             Address           words
0  Ramesh  297 shukrawar peth  shukrawar peth
1  Suresh    200 ravivar peth    ravivar peth
2  Sanket   090 shanivar peth   shanivar peth

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Extract hashtags

Python Practice › Pandas Programs › Program 77

Extract hashtags

EasyPandas Programs · 77 of 95

Write a pandas program to extract hashtag words from tweets.

Output
                  Tweets             tags
0             Pune #love          [#love]
1          #boycottmovie  [#boycottmovie]
2  enjoying #peace #calm  [#peace, #calm]

Approach

  1. str.findall(r"#\w+").

Solution

Python
import pandas as pddf = pd.DataFrame({"Tweets": ["Pune #love", "#boycottmovie", "enjoying #peace #calm"]})df["tags"] = df["Tweets"].str.findall(r"#\w+")print(df)
Output
                  Tweets             tags
0             Pune #love          [#love]
1          #boycottmovie  [#boycottmovie]
2  enjoying #peace #calm  [#peace, #calm]

Learn the concept

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

More pandas programs

← All 95 Pandas Programs