Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Python Pandas String and Regular Expression: Exercises, Practice, Solution

[An editor is available at the bottom of the page to write and execute the scripts.]

Pandas String and Regular Expression [ 41 exercises with solution]

1. Write a Pandas program to convert all the string values to upper, lower cases in a given pandas series. Also find the length of the string values. Go to the editor
Click me to see the sample solution

2. Write a Pandas program to remove whitespaces, left sided whitespaces and right sided whitespaces of the string values of a given pandas series. Go to the editor
Click me to see the sample solution

3. Write a Pandas program to add leading zeros to the integer column in a pandas series and makes the length of the field to 8 digit. Go to the editor
Click me to see the sample solution

4. Write a Pandas program to add leading zeros to the character column in a pandas series and makes the length of the field to 8 digit. Go to the editor
Click me to see the sample solution

5. Write a Pandas program to capitalize all the string values of specified columns of a given DataFrame. Go to the editor
Click me to see the sample solution

6. Write a Pandas program to count of occurrence of a specified substring in a DataFrame column. Go to the editor
Click me to see the sample solution

7. Write a Pandas program to find the index of a given substring of a DataFrame column. Go to the editor
Click me to see the sample solution

8. Write a Pandas program to find the index of a substring of DataFrame with beginning and end position. Go to the editor
Click me to see the sample solution

9. Write a Pandas program to check whether alpha numeric values present in a given column of a DataFrame. Go to the editor
Note: isalnum() function returns True if all characters in the string are alphanumeric and there is at least one character, False otherwise.
Click me to see the sample solution

10. Write a Pandas program to check whether alphabetic values present in a given column of a DataFrame. Go to the editor
Note: isalpha() returns True if all characters in the string are alphabetic and there is at least one character, False otherwise.
Click me to see the sample solution

11. Write a Pandas program to check whether only numeric values present in a given column of a DataFrame.Go to the editor
Click me to see the sample solution

12. Write a Pandas program to check whether only lower case or upper case is present in a given column of a DataFrame. Go to the editor
Click me to see the sample solution

13. Write a Pandas program to check whether only proper case or title case is present in a given column of a DataFrame. Go to the editor
Click me to see the sample solution

14. Write a Pandas program to check whether only space is present in a given column of a DataFrame. Go to the editor
Click me to see the sample solution

15. Write a Pandas program to get the length of the string present of a given column in a DataFrame. Go to the editor
Click me to see the sample solution

16. Write a Pandas program to get the length of the integer of a given column in a DataFrame. Go to the editor
Click me to see the sample solution

17. Write a Pandas program to check if a specified column starts with a specified string in a DataFrame. Go to the editor
Click me to see the sample solution

18. Write a Pandas program to swap the cases of a specified character column in a given DataFrame. Go to the editor
Click me to see the sample solution

19. Write a Pandas program to convert a specified character column in upper/lower cases in a given DataFrame. Go to the editor
Click me to see the sample solution

20. Write a Pandas program to convert a specified character column in title case in a given DataFrame. Go to the editor
Click me to see the sample solution

21. Write a Pandas program to replace arbitrary values with other values in a given DataFrame. Go to the editor
Click me to see the sample solution

22. Write a Pandas program to replace more than one value with other values in a given DataFrame. Go to the editor
Click me to see the sample solution

23. Write a Pandas program to split a string of a column of a given DataFrame into multiple columns. Go to the editor
Click me to see the sample solution

24. Write a Pandas program to extract email from a specified column of string type of a given DataFrame. Go to the editor
Click me to see the sample solution

25. Write a Pandas program to extract hash attached word from twitter text from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

26. Write a Pandas program to extract word mention someone in tweets using @ from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

27. Write a Pandas program to extract only number from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

28. Write a Pandas program to extract only phone number from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

29. Write a Pandas program to extract year between 1800 to 2200 from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

30. Write a Pandas program to extract only non alphanumeric characters from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

31. Write a Pandas program to extract only punctuations from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

32. Write a Pandas program to remove repetitive characters from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

33. Write a Pandas program to extract numbers greater than 940 from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

34. Write a Pandas program to extract numbers less than 100 from the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

35. Write a Pandas program to check whether two given words present in a specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

36. Write a Pandas program to extract date (format: mm-dd-yyyy) from a given column of a given DataFrame. Go to the editor
Click me to see the sample solution

37. Write a Pandas program to extract only words from a given column of a given DataFrame. Go to the editor
Click me to see the sample solution

38. Write a Pandas program to extract the sentences where a specific word is present in a given column of a given DataFrame. Go to the editor
Click me to see the sample solution

39. Write a Pandas program to extract the unique sentences from a given column of a given DataFrame. Go to the editor
Click me to see the sample solution

40. Write a Pandas program to extract words starting with capital words from a given column of a given DataFrame. Go to the editor
Click me to see the sample solution

41. Write a Pandas program to remove the html tags within the specified column of a given DataFrame. Go to the editor
Click me to see the sample solution

Python Code Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m