Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to replace last 4 digits in Pandas Dataframe by 0101 if they are 9999 (Python)

I have a Dataframe that looks like this:

OrdNo  year
1     20059999
2     20070830
3     20070719
4     20030719
5     20039999
6     20070911
7     20050918
8     20070816
9     20069999

How to replace last 4 digits in the Pandas Dataframe by 0101 if they are 9999?

Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming your year column is of type str:

df["year"] = df["year"].str.replace("(9999)$", "0101")

If it is of numeric type

df["year"] = pd.to_numeric(df["year"].astype(str).str.replace("(9999)$", "0101"), errors="coerce")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading