1

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  
  • Reverse the names. `Test['IntOnly'] = Test['Work_Experience ']...`. In the second case you use the numbers in the existing column as indexes in an empty array – Panagiotis Kanavos Jan 10 '23 at 08:16
  • @PanagiotisKanavos - Sorry there was typo error have edited it throwing this error - 'Series' object has no attribute 'select_dtypes' – RajatKapoor350 Jan 10 '23 at 08:42
  • Instead of trying random code, read the docs. [select_dtype](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.select_dtypes.html) is a Dataframe method, it doesn't work on columns. It doesn't change types either. That's what `astype` does – Panagiotis Kanavos Jan 10 '23 at 08:45
  • Does this answer your question? [Change column type in pandas](https://stackoverflow.com/questions/15891038/change-column-type-in-pandas) – Panagiotis Kanavos Jan 10 '23 at 08:45
  • @PanagiotisKanavos - I am not looking to change the Data Type, I have a Column which has both integer & string. I simply want to place integer value in new column and remaining String values should be NaN – RajatKapoor350 Jan 10 '23 at 08:52
  • Which means changing the data type. You need `astype` or `to_numeric` for that. The functions return a *new* series that can either be added to the original dataset with the same or a new name. Both functions accept an `error` parameter that specifies how to handle non-numeric values – Panagiotis Kanavos Jan 10 '23 at 08:57

1 Answers1

3

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
Niyaz
  • 797
  • 1
  • 8
  • 18