Palindromes

In [1]:
import pandas as pd

In [7]:
# http://ukgaz.ben-daglish.net/cgi-bin/ukgaz.cgi?page=download
db = pd.read_csv('ukgaz/places.csv', \
                 encoding='iso-8859-1', \
                 index_col='id')
db.head()
Out[7]:
name countyname county easting northing pce pcn posttown sortname commcode ufi statscode osm_id geonames_id
id
1 A'Chill Highland 65.0 NaN NaN 127034.0 805208.0 NaN ACHILL NaN NaN NaN NaN NaN
2 Ab Kettleby Melton 376.0 472302.0 323168.0 474500.0 321234.0 16377.0 ABKETTLEBY 31UG001 -2587812.0 NaN 2885.0 2657777.0
3 Abbas Combe South Somerset 432.0 370000.0 122000.0 371151.0 122269.0 23574.0 ABBASCOMBE 40UD001 NaN NaN NaN NaN
4 Abberley Malvern Hills 478.0 374998.0 266981.0 374738.0 258967.0 26834.0 ABBERLEY 47UC001 -2587708.0 NaN NaN 2657881.0
5 Abberton Colchester 314.0 600933.0 219037.0 594454.0 216654.0 5806.0 ABBERTON 22UG001 -2587709.0 NaN NaN 2657880.0
In [21]:
def ispalin(n):
    if n.lower() == n.lower()[::-1]:
        return True
    else:
        return False
In [41]:
palin = db.loc[:, 'name'].apply(ispalin)

long_palins = db.loc[palin==True, 'name']\
                .apply(len)\
                .sort_values(ascending=False)\
                .index
db.loc[long_palins, 'name']
Out[41]:
id
10059    Glenelg
19979     Rattar
18176     Notton
18175     Notton
14360     Lennel
11087     Hannah
36493      Navan
8778         Eye
8777         Eye
8776         Eye
Name: name, dtype: object

In [55]:
import os

p = 'opname_csv_gb/DATA/'
reader = lambda f: pd.read_csv(p+f, header=None).loc[:, 2]

db = pd.concat(map(reader, os.listdir(p)))
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (3,4,5) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (4,32) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (4) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (31,33) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (4,31,32) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (31,32,33) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (4,31,33) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (33) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (4,31,32,33) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (32,33) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (32) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (3,4,5,32,33) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (31) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (4,16,17,31,32,33) have mixed types. Specify dtype option on import or set low_memory=False.
  
/Users/dani/anaconda/envs/gds/lib/python3.5/site-packages/ipykernel_launcher.py:6: DtypeWarning: Columns (3,4,5,16,17,31,32,33) have mixed types. Specify dtype option on import or set low_memory=False.
  
In [75]:
palin = db.apply(ispalin)

lens = db.loc[palin==True]\
  .apply(len)
In [76]:
pd.DataFrame({'Name': db.loc[palin==True], \
              'len': lens})\
            .sort_values('len', ascending=False)
Out[76]:
Name len
1768 GL7 7LG 7
8306 SL4 4LS 7
763 GL8 8LG 7
1216 SP1 1PS 7
2404 SN2 2NS 7
2422 SN4 4NS 7
5059 SN6 6NS 7
9344 SN3 3NS 7
12162 Glenelg 7
995 RG9 9GR 7
1139 SL6 6LS 7
5073 BA2 2AB 7
1162 HP6 6PH 7
7083 SL9 9LS 7
8158 SL2 2LS 7
685 TR1 1RT 7
281 PL1 1LP 7
3090 PL9 9LP 7
5218 PL2 2LP 7
502 PL6 6LP 7
373 SP5 5PS 7
2527 BS4 4SB 7
167 LD4 4DL 7
1293 NN5 5NN 7
1402 WR6 6RW 7
8 Glenelg 7
2964 GL6 6LG 7
2966 GL3 3LG 7
4958 GL5 5LG 7
5196 GL1 1LG 7
... ... ...
525 DE4 4ED 7
18446 NG5 5GN 7
12124 HX1 1XH 7
5263 BD8 8DB 7
444 BD2 2DB 7
902 NG4 4GN 7
204 PR3 3RP 7
522 BB1 1BB 7
4463 BB3 3BB 7
5462 BB4 4BB 7
1003 LE5 5EL 7
11652 BB5 5BB 7
335 LE2 2EL 7
12533 BL9 9LB 7
10047 BB9 9BB 7
453 HD1 1DH 7
3554 HD8 8DH 7
3706 HD2 2DH 7
4976 HD3 3DH 7
9981 HD9 9DH 7
2684 LE7 7EL 7
25 Rattar 6
3 Lennel 6
33 Notton 6
71 Notton 6
22 Hannah 6
16 Notton 6
33 Lapal 5
26 Eye 3
2 Eye 3

178 rows × 2 columns