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

Alphabetic-only check

Python Practice › Pandas Programs › Program 74

Alphabetic-only check

EasyPandas Programs · 74 of 95

Write a pandas program to check which values contain only letters.

Input
["Pass", "88", "First class", "90", "Distinction"]
Output
         Marks  is_alpha
0         Pass      True
1           88     False
2  First class     False
3           90     False
4  Distinction      True

Approach

  1. str.isalpha(); spaces make it False.

Solution

Python
import pandas as pddf = pd.DataFrame({"Marks": ["Pass", "88", "First class", "90", "Distinction"]})df["is_alpha"] = df["Marks"].str.isalpha()print(df)
Output
         Marks  is_alpha
0         Pass      True
1           88     False
2  First class     False
3           90     False
4  Distinction      True

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Numeric-only check

Python Practice › Pandas Programs › Program 73

Numeric-only check

EasyPandas Programs · 73 of 95

Write a pandas program to check which values contain only digits.

Input
["Pass", "88", "2nd class", "90", "Distinction"]
Output
         Marks  is_numeric
0         Pass       False
1           88        True
2    2nd class       False
3           90        True
4  Distinction       False

Approach

  1. str.isnumeric().

Solution

Python
import pandas as pddf = pd.DataFrame({"Marks": ["Pass", "88", "2nd class", "90", "Distinction"]})df["is_numeric"] = df["Marks"].str.isnumeric()print(df)
Output
         Marks  is_numeric
0         Pass       False
1           88        True
2    2nd class       False
3           90        True
4  Distinction       False

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Lowercase-only check

Python Practice › Pandas Programs › Program 72

Lowercase-only check

EasyPandas Programs · 72 of 95

Write a pandas program to check which values are fully lowercase.

Input
["kate", "jason", "ROBERT", "MARK", "dwyane"]
Output
     Name  is_lower
0    kate      True
1   jason      True
2  ROBERT     False
3    MARK     False
4  dwyane      True

Approach

  1. str.islower().

Solution

Python
import pandas as pddf = pd.DataFrame({"Name": ["kate", "jason", "ROBERT", "MARK", "dwyane"]})df["is_lower"] = df["Name"].str.islower()print(df)
Output
     Name  is_lower
0    kate      True
1   jason      True
2  ROBERT     False
3    MARK     False
4  dwyane      True

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Uppercase-only check

Python Practice › Pandas Programs › Program 71

Uppercase-only check

EasyPandas Programs · 71 of 95

Write a pandas program to check which values are fully uppercase.

Input
["Kate", "Jason", "ROBERT", "MARK", "Dwyane"]
Output
     Name  is_upper
0    Kate     False
1   Jason     False
2  ROBERT      True
3    MARK      True
4  Dwyane     False

Approach

  1. str.isupper().

Solution

Python
import pandas as pddf = pd.DataFrame({"Name": ["Kate", "Jason", "ROBERT", "MARK", "Dwyane"]})df["is_upper"] = df["Name"].str.isupper()print(df)
Output
     Name  is_upper
0    Kate     False
1   Jason     False
2  ROBERT      True
3    MARK      True
4  Dwyane     False

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Capitalise text values

Python Practice › Pandas Programs › Program 70

Capitalise text values

EasyPandas Programs · 70 of 95

Write a pandas program to capitalise the text values of a column.

Input
["cricket", "Yes", "pass", "FAIL"]
Output
0    Cricket
1        Yes
2       Pass
3       Fail
dtype: str

Approach

  1. str.capitalize().

Solution

Python
import pandas as pds = pd.Series(["cricket", "Yes", "pass", "FAIL"])print(s.str.capitalize())
Output
0    Cricket
1        Yes
2       Pass
3       Fail
dtype: str

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Select text columns

Python Practice › Pandas Programs › Program 69

Select text columns

EasyPandas Programs · 69 of 95

Write a pandas program to select the text columns.

Output
   name   city
0  Alex   Pune
1  John  Delhi

Approach

  1. select_dtypes(include=...).

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John"], "city": ["Pune", "Delhi"], "age": [30, 27]})print(df.select_dtypes(exclude="number"))
Output
   name   city
0  Alex   Pune
1  John  Delhi

Line by line

Line 3

Excluding numeric columns works on both old (object) and new (str) text types.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Reverse column order

Python Practice › Pandas Programs › Program 68

Reverse column order

EasyPandas Programs · 68 of 95

Write a pandas program to reverse the order of columns.

Output
   salary  age  name
0   50000   30  Alex
1   65000   27  John

Approach

  1. df[df.columns[::-1]].

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John"], "age": [30, 27], "salary": [50000, 65000]})print(df[df.columns[::-1]])
Output
   salary  age  name
0   50000   30  Alex
1   65000   27  John

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Reverse row order

Python Practice › Pandas Programs › Program 67

Reverse row order

EasyPandas Programs · 67 of 95

Write a pandas program to reverse the order of rows.

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

Approach

  1. iloc[::-1].

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs