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 do u filter data in pandas by columns of different datatype

I’m having this issue where I got one date table of datetime dtype and another column of float type. Example – DateColumn (year-month-day) and Salary of type float.
How can I take all the rows of lets say 2019 where the salary is higher than 2000.

>Solution :

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

Say you have the data in this way:

date = pd.date_range("2019-01-01", periods=500, freq="D")
salary = np.random.randint(100, 10000, 500)
df = pd.DataFrame({"date": date, "salary": salary})

Then you can just select:

 df[(df["date"].dt.year == 2019) & (df["salary"] > 2000)]

If your date column is a string (df["date"] = df["date"].astype(str)), then you can do:

df[(df["date"] >= "2019-01-01") & (df["date"] <= "2019-12-31") & (df["salary"] > 2000)]
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