w3resource

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


This resource offers a total of 205 Pandas String and Regular Expression problems for practice. It includes 41 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

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


1. Convert String Cases & Length

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.

Click me to see the sample solution


2. Remove Whitespaces

Write a Pandas program to remove whitespaces, left sided whitespaces and right sided whitespaces of the string values of a given pandas series.

Click me to see the sample solution


3. Add Leading Zeros to Integers

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.

Click me to see the sample solution


4. Add Leading Zeros to Characters

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.

Click me to see the sample solution


5. Capitalize Specified Columns

Write a Pandas program to capitalize all the string values of specified columns of a given DataFrame.

Click me to see the sample solution


6. Count Substring Occurrence

Write a Pandas program to count of occurrence of a specified substring in a DataFrame column.

Click me to see the sample solution


7. Find Substring Index

Write a Pandas program to find the index of a given substring of a DataFrame column.

Click me to see the sample solution


8. Find Substring with Positions

Write a Pandas program to find the index of a substring of DataFrame with beginning and end position.

Click me to see the sample solution


9. Check Alphanumeric in Column

Write a Pandas program to check whether alpha numeric values present in a given column of a DataFrame.

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. Check Alphabetic in Column

Write a Pandas program to check whether alphabetic values present in a given column of a DataFrame.

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. Check Numeric Only in Column

Write a Pandas program to check whether only numeric values present in a given column of a DataFrame.

Click me to see the sample solution


12. Check Case Consistency (Lower/Upper)

Write a Pandas program to check whether only lower case or upper case is present in a given column of a DataFrame.

Click me to see the sample solution


13. Check Title/Proper Case

Write a Pandas program to check whether only proper case or title case is present in a given column of a DataFrame.

Click me to see the sample solution


14. Check Only Spaces in Column

Write a Pandas program to check whether only space is present in a given column of a DataFrame.

Click me to see the sample solution


15. Get Length of String in Column

Write a Pandas program to get the length of the string present of a given column in a DataFrame.

Click me to see the sample solution


16. Get Length of Integer in Column

Write a Pandas program to get the length of the integer of a given column in a DataFrame.

Click me to see the sample solution


17. Check Column Starts with Specified String

Write a Pandas program to check if a specified column starts with a specified string in a DataFrame.

Click me to see the sample solution


18. Swap Cases of Character Column

Write a Pandas program to swap the cases of a specified character column in a given DataFrame.

Click me to see the sample solution


19. Convert Character Column to Upper/Lower

Write a Pandas program to convert a specified character column in upper/lower cases in a given DataFrame.

Click me to see the sample solution


20. Convert Character Column to Title Case

Write a Pandas program to convert a specified character column in title case in a given DataFrame.

Click me to see the sample solution


21. Replace Arbitrary Values

Write a Pandas program to replace arbitrary values with other values in a given DataFrame.

Click me to see the sample solution


22. Replace Multiple Values

Write a Pandas program to replace more than one value with other values in a given DataFrame.

Click me to see the sample solution


23. Split Column String into Multiple Columns

Write a Pandas program to split a string of a column of a given DataFrame into multiple columns.

Click me to see the sample solution


24. Extract Email from Column

Write a Pandas program to extract email from a specified column of string type of a given DataFrame.

Click me to see the sample solution


25. Extract Hashtags from Twitter Text

Write a Pandas program to extract hash attached word from twitter text from the specified column of a given DataFrame.

Click me to see the sample solution


26. Extract Mentions (@) from Tweets

Write a Pandas program to extract word mention someone in tweets using @ from the specified column of a given DataFrame.

Click me to see the sample solution


27. Extract Only Numbers from Column

Write a Pandas program to extract only number from the specified column of a given DataFrame.

Click me to see the sample solution


28. Extract Phone Numbers from Column

Write a Pandas program to extract only phone number from the specified column of a given DataFrame.

Click me to see the sample solution


29. Extract Year between 1800 and 2200

Write a Pandas program to extract year between 1800 to 2200 from the specified column of a given DataFrame.

Click me to see the sample solution


30. Extract Non-Alphanumeric Characters

Write a Pandas program to extract only non alphanumeric characters from the specified column of a given DataFrame.

Click me to see the sample solution


31. Extract Punctuations Only

Write a Pandas program to extract only punctuations from the specified column of a given DataFrame.

Click me to see the sample solution


32. Remove Repetitive Characters

Write a Pandas program to remove repetitive characters from the specified column of a given DataFrame.

Click me to see the sample solution


33. Extract Numbers Greater Than 940

Write a Pandas program to extract numbers greater than 940 from the specified column of a given DataFrame.

Click me to see the sample solution


34. Extract Numbers Less Than 100

Write a Pandas program to extract numbers less than 100 from the specified column of a given DataFrame.

Click me to see the sample solution


35. Check Two Words Presence in Column

Write a Pandas program to check whether two given words present in a specified column of a given DataFrame.

Click me to see the sample solution


36. Extract Date in mm-dd-yyyy Format

Write a Pandas program to extract date (format: mm-dd-yyyy) from a given column of a given DataFrame.

Click me to see the sample solution


37. Extract Only Words from Column

Write a Pandas program to extract only words from a given column of a given DataFrame.

Click me to see the sample solution


38. Extract Sentences Containing Specific Word

Write a Pandas program to extract the sentences where a specific word is present in a given column of a given DataFrame.

Click me to see the sample solution


39. Extract Unique Sentences

Write a Pandas program to extract the unique sentences from a given column of a given DataFrame.

Click me to see the sample solution


40. Extract Words Starting with Capital Letters

Write a Pandas program to extract words starting with capital words from a given column of a given DataFrame.

Click me to see the sample solution


41. Remove Tags from Column

Write a Pandas program to remove the html tags within the specified column of a given DataFrame.

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



Follow us on Facebook and Twitter for latest update.