- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in Python to filter City column elements by removing the unique prefix in a given dataframe
Assume you have a dataframe, the result for removing unique prefix city names are,
Id City 2 3 Kolkata 3 4 Hyderabad 6 7 Haryana 8 9 Kakinada 9 10 Kochin
To solve this, we will follow the steps given below −
Solution
Define a dataframe
Create an empty list to append all the city column values first char’s,
l = [] for x in df['City']: l.append(x[0])
Create another empty list to filter repeated char.
Set for loop and if condtion to append unique char’s. It is defined below,
l1 = [] for j in l: if(l.count(j)>1): if(j not in l1): l1.append(j)
Create another empty list. Set for loop to access city column values and check the elements first char is present in l1 then append it to another list.
l2 = [] for x in df['City']: if(x[0] in l1): l2.append(x)
Finally, verify the l2 elements is present in the city column or not and print the dataframe using isin().
df[df['City'].isin(l2)]
Example
Let’s check the following code to get a better understanding −
import pandas as pd df = pd.DataFrame({'Id':[1,2,3,4,5,6,7,8,9,10], 'City':['Chennai','Delhi','Kolkata','Hyderabad','Pune','Mumbai','Haryana','B engaluru','Kakinada','Kochin'] }) l = [] for x in df['City']: l.append(x[0]) l1 = [] for j in l: if(l.count(j)>1): if(j not in l1): l1.append(j) l2 = [] for x in df['City']: if(x[0] in l1): l2.append(x) print(df[df['City'].isin(l2)])
Output
Id City 2 3 Kolkata 3 4 Hyderabad 6 7 Haryana 8 9 Kakinada 9 10 Kochin
- Related Articles
- Write a Python program to sort a given DataFrame by name column in descending order
- Write a Python code to filter palindrome names in a given dataframe
- Write a program in Python to filter only integer elements in a given series
- Python - Add a prefix to column names in a Pandas DataFrame
- Write a program in Python to modify the diagonal of a given DataFrame by 1
- Write a program in Python to print the length of elements in all column in a dataframe using applymap
- 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 store the city and state names that start with ‘k’ in a given DataFrame into a new CSV file
- Write a program in Python to find which column has the minimum number of missing values in a given dataframe
- Write a program in Python to covert the datatype of a particular column in a dataframe
- Write a program in Python to convert a given dataframe to a LaTex document
- Write a program in Python to perform flatten the records in a given dataframe by C and F order
- Write a program in Python to transpose the index and columns in a given DataFrame

Advertisements