Search code examples
pythonpandasdataframeleading-zero

Pandas dataframe search and modify leading zero on postal codes


I have a pandas DataFrame database of location and postal code addresses. But postal codes have been interpreted from Excel reading. For example, in France postal addresses are a department code followed by city code, e.g. 75600 (75 = 'Paris region', 600 = 'inner city').

But in some first postal codes, e.g. 01200 it have interpreted like 1200. How can I search integer values below 10000 and modify them? Or how to preserve the first zero. How to search and replace in a dataframe and use the content (to modify it)?


Solution

  • So, as I understood, one possible solution is to preserve the firs 0 when postal code < 10000. Is that it? One way is to convert all entries to string.

    import pandas as pd
    # example with only two locations in column 0 of a DataFrame 
    df = pd.DataFrame([75600, 1200])
    # convert all entries in column 0 to string, adding '0' to the beginning when postal code < 10000
    df = df[0].apply(lambda x: '0' + str(x) if x < 10000 else str(x))
    

    If this is not the solution you want please let me know.