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