RajatKapoor350
RajatKapoor350

Reputation: 47

Extract INT64 Datatype to new column but it does not extract String Datatype to New Column Python

I have the following Table Test

Work_Experience

1  
2  
3  
4  
3 Month  
4 months  
5 Month  

Now i want that a new column should be created IntOnly where first four values (of datatype Int64) should be there and for remaining last 3 rows it should display 'NaN'

I have tried two main codes

Test['IntOnly'] = Test['Work_Experience'].select_dtypes(include='number')

It's throwing 'Series' object has no attribute 'select_dtypes'

IntOnly=[]  
for i in Test.Work_Experience:  
    if Test[i].dtype == 'int64':  
        IntOnly.append(i)  

Its throwing Key Error : 1

Following should be the output

Work_Experience         IntOnly

1                           1  
2                           2  
3                           3  
4                           4  
3 Month                     NaN  
4 months                    NaN  
5 Month                     NaN  

Upvotes: 1

Views: 141

Answers (1)

Niyaz
Niyaz

Reputation: 927

Try this:

df = pd.DataFrame({
    "Work_Experience": [1,2,3,4,'3 Month','4 months','5 Month']
})
df["IntOnly"] = pd.to_numeric(df["Work_Experience"], errors='coerce')
df

Upvotes: 3

Related Questions