
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a Python code to filter palindrome names in a given dataframe
Result for printing palindrome names are −
Palindrome names are: Id Name 0 1 bob 2 3 hannah
To solve this, we will follow the below approaches −
Solution 1
Define a dataframe
Create list comprehension inside set for loop to access all the values from df[‘Name’] column using i variable and set if condition to compare i==i[::-1] then add i value to the list
l = [ i for i in df['Name'] if(i==i[::-1])]
Finally, check the list values present in the df[‘Name’] column using isin()
df[df['Name'].isin(l)]
Example
Let’s check the following code to get a better understanding −
import pandas as pd data = {'Id':[1,2,3,4,5],'Name':['bob','peter','hannah','james','david']} df = pd.DataFrame(data) print("DataFrame is:\n", df) l = [ i for i in df['Name'] if(i==i[::-1])] print("Palindrome names are:\n", df[df['Name'].isin(l)])
Output
DataFrame is: Id Name 0 1 bob 1 2 peter 2 3 hannah 3 4 james 4 5 david Palindrome names are: Id Name 0 1 bob 2 3 hannah
Solution 2
Define a dataframe
Apply lambda filter function to compare df[‘Name’] each values with reversed function returns same result or not. If the values are matched then store it as result list.
result = list(filter(lambda x:(x=="".join(reversed(x))),df['Name']
Finally, check the list values present in the df[‘Name’] column using isin()
df[df['Name'].isin(result)]
Example
Let’s check the following code to get a better understanding −
import pandas as pd data = {'Id':[1,2,3,4,5],'Name':['bob','peter','hannah','james','david']} df = pd.DataFrame(data) print("DataFrame is:\n", df) result = list(filter(lambda x:(x=="".join(reversed(x))),df['Name'])) print("Palindrome names are:\n", df[df['Name'].isin(result)])
Output
DataFrame is: Id Name 0 1 bob 1 2 peter 2 3 hannah 3 4 james 4 5 david Palindrome names are: Id Name 0 1 bob 2 3 hannah
- Related Articles
- Write a Python code to rename the given axis in a dataframe
- Write a Python code to swap last two rows in a given dataframe
- Write a Python code to fill all the missing values in a given dataframe
- Write a Python code to select any one random row from a given DataFrame
- Write a Python code to combine two given series and convert it to a dataframe
- Write a Python code to find the second lowest value in each column in a given dataframe
- Write a program in Python to filter City column elements by removing the unique prefix in a given dataframe
- Write a program in Python to print the ‘A’ grade students’ names from a DataFrame
- Write a program in Python to filter valid dates in a given series
- Write a program in Python to filter armstrong numbers in a given series
- Write a Python program to reshape a given dataframe in different ways
- Write a program in Python to convert a given dataframe to a LaTex document
- Write a program in Python to filter only integer elements in a given series
- Write a program in Python to localize Asian timezone for a given dataframe
- Write a program in Python to remove first duplicate rows in a given dataframe
