Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

Col=0 is an object datatype through that  I wanted to iterate and find integer like 2010,2018,2017 etc in my col=0, should I assign all the values in the column to zero like a year?

My DF: 0 1

Nan Banks National Banks Axis Bank Nan ICICI Nan PNB 2010 KYB Nan Indus Ind Nan Karur

My desired output: 0 1

2010 Banks 2010 Axis Bank 2010 ICICI 2010 PNB 2010 KYB 2010 Indus Ind 2010 Karur

1 Answer

0 votes
by (36.8k points)
edited by

The following are the steps to iterate and find the integer:

1.Using to_numeric() function convert the pd column to numeric

2. Convert all numbers outside range to NaN values by using series. where and masking with series.isin

s = pd.to_numeric(df[0], errors='coerce'

df[0] = s.where(s.isin(range(2010, 2020))) 

print (df)

0 1 

0 NaN Banks 

1 NaN Axis Bank 

2 NaN ICICI 

3 NaN PNB 

4 2010.0 KYB 

5 NaN Indus Ind 

6 NaN Karur

To learn Python programming in data science and improve your technical skills in Data Science with Python course

Browse Categories

...