1.10. About Chaining
1.10.1. SetUp
>>> import pandas as pd
1.10.2. Inplace
>>> s = pd.Series([1, pd.NA, 5, pd.NA, 1, 2, 1])
>>> s.fillna(0, inplace=True)
>>> s.drop([2,4,6], inplace=True)
>>> s.drop_duplicates(inplace=True)
>>> s.reset_index(drop=True, inplace=True)
>>> s
0 1
1 0
2 2
dtype: int64
1.10.3. Endl
>>> s = pd.Series([1, pd.NA, 5, pd.NA, 1, 2, 1]) \
... .fillna(0) \
... .drop([2,4,6]) \
... .drop_duplicates() \
... .reset_index(drop=True)
>>>
>>> s
0 1
1 0
2 2
dtype: int64
1.10.4. Chain
>>> s = (pd.Series([1, pd.NA, 5, pd.NA, 1, 2, 1])
... .fillna(0)
... .drop([2,4,6])
... .drop_duplicates()
... .reset_index(drop=True))
>>>
>>> s
0 1
1 0
2 2
dtype: int64
1.10.5. Further Reading
1.10.6. Use Case - 1
>>> DATA = [
... 'ul.Mieszka II',
... 'UL. Zygmunta III WaZY',
... ' bolesława chrobrego ',
... 'ul Jana III SobIESkiego',
... '\tul. Jana trzeciego Sobieskiego',
... 'ulicaJana III Sobieskiego',
... 'UL. JA NA 3 SOBIES KIEGO',
... 'ULICA JANA III SOBIESKIEGO ',
... 'ULICA. JANA III SOBIeskieGO',
... ' Jana 3 Sobieskiego ',
... 'Jana III Sobi eskiego ',
... ]
>>>
>>>
>>> def clean(text):
... text = text.strip()
... text = text.upper()
... text = text.replace('\t', '')
... text = text.replace('\n', '')
... text = text.replace(' ', '')
... text = text.replace(' ', '')
... text = text.replace(' ', '')
... text = text.replace('.', '')
... text = text.replace(',', '')
... text = text.replace('\\', '')
... text = text.replace('ULICA', '')
... text = text.replace('UL', '')
... text = text.replace('TRZECIEGO', 'III')
... text = text.replace('3', 'III')
... text = text.title()
... text = text.replace('Iii', 'III')
... text = text.replace('Ii', 'II')
... return text.strip()
>>>
>>>
>>> s = pd.Series(DATA)
>>> s.apply(clean)
0 Mieszka II
1 Zygmunta III Wazy
2 Bolesława Chrobrego
3 Jana III Sobieskiego
4 Jana III Sobieskiego
5 Jana III Sobieskiego
6 Jana III Sobieskiego
7 Jana III Sobieskiego
8 Jana III Sobieskiego
9 Jana III Sobieskiego
10 Jana III Sobieskiego
dtype: object
>>> DATA = [
... 'ul.Mieszka II',
... 'UL. Zygmunta III WaZY',
... ' bolesława chrobrego ',
... 'ul Jana III SobIESkiego',
... '\tul. Jana trzeciego Sobieskiego',
... 'ulicaJana III Sobieskiego',
... 'UL. JA NA 3 SOBIES KIEGO',
... 'ULICA JANA III SOBIESKIEGO ',
... 'ULICA. JANA III SOBIeskieGO',
... ' Jana 3 Sobieskiego ',
... 'Jana III Sobi eskiego ',
... ]
>>>
>>> def clean(text):
... return (text
... .strip()
... .upper()
... .replace('\t', '')
... .replace('\n', '')
... .replace(' ', '')
... .replace(' ', '')
... .replace(' ', '')
... .replace('.', '')
... .replace(',', '')
... .replace('\\', '')
... .replace('ULICA', '')
... .replace('UL', '')
... .replace('TRZECIEGO', 'III')
... .replace('3', 'III')
... .title()
... .replace('Iii', 'III')
... .replace('Ii', 'II')
... .strip())
>>>
>>>
>>> s = pd.Series(DATA)
>>> s.apply(clean)
0 Mieszka II
1 Zygmunta III Wazy
2 Bolesława Chrobrego
3 Jana III Sobieskiego
4 Jana III Sobieskiego
5 Jana III Sobieskiego
6 Jana III Sobieskiego
7 Jana III Sobieskiego
8 Jana III Sobieskiego
9 Jana III Sobieskiego
10 Jana III Sobieskiego
dtype: object