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

Extract emails

Python Practice › Pandas Programs › Program 76

Extract emails

EasyPandas Programs · 76 of 95

Write a pandas program to extract email addresses from a column.

Output
              Company_mail             email
0        TCS tcs@yahoo.com     tcs@yahoo.com
1   Apple apple@icloud.com  apple@icloud.com
2  Google google@gmail.com  google@gmail.com

Approach

  1. str.extract with a regex group.

Solution

Python
import pandas as pddf = pd.DataFrame({"Company_mail": ["TCS tcs@yahoo.com", "Apple apple@icloud.com", "Google google@gmail.com"]})df["email"] = df["Company_mail"].str.extract(r"([\w.+-]+@[\w-]+\.[\w.]+)", expand=False)print(df)
Output
              Company_mail             email
0        TCS tcs@yahoo.com     tcs@yahoo.com
1   Apple apple@icloud.com  apple@icloud.com
2  Google google@gmail.com  google@gmail.com

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Number of digits

Python Practice › Pandas Programs › Program 75

Number of digits

EasyPandas Programs · 75 of 95

Write a pandas program to count the digits of each integer in a column.

Input
[55000, 75000, 330000, 10000]
Output
    Sales  Sales_length
0   55000             5
1   75000             5
2  330000             6
3   10000             5

Approach

  1. Convert to string and use str.len().

Solution

Python
import pandas as pddf = pd.DataFrame({"Sales": [55000, 75000, 330000, 10000]})df["Sales_length"] = df["Sales"].astype(str).str.len()print(df)
Output
    Sales  Sales_length
0   55000             5
1   75000             5
2  330000             6
3   10000             5

Learn the concept

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

More pandas programs

← All 95 Pandas Programs