2

The below column in data frame needs to be converted to int:

dsAttendEnroll.District.head()

0    DISTRICT 01
1    DISTRICT 02
2    DISTRICT 03
3    DISTRICT 04
4    DISTRICT 05
Name: District, dtype: object

Using astype gives the below error, how can this be done ?

dsAttendEnroll.District = dsAttendEnroll.District.map(lambda x: x[-2:]).astype(int)

ValueError: invalid literal for long() with base 10: 'LS'

3
  • 4
    that means your data has something that ends with LS... Commented Sep 26, 2016 at 17:36
  • 1
    I think you first need to decide what you want to do with the LS data. Do you want to discard it or split it into a separate column (as suggested below)? Is LS valid or invalid? Commented Sep 26, 2016 at 18:21
  • Thanks filtering out the incorrect data solved the problem. Commented Sep 26, 2016 at 19:27

2 Answers 2

3

You can use split with selecting second lists by str[1] with to_numeric, where is parameter errors='coerce' - it convert not numeric values to NaN:

print (df)
      District
0  DISTRICT 01
1  DISTRICT 02
2  DISTRICT 03
3  DISTRICT 04
4  DISTRICT 05
5  DISTRICT LS

print (df.District.str.split().str[1])
0    01
1    02
2    03
3    04
4    05
5    LS
Name: District, dtype: object

print (pd.to_numeric(df.District.str.split().str[1], errors='coerce'))
0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    NaN
Name: District, dtype: float64

Another solution with slice 2 last chars:

print (df.District.str[-2:])
0    01
1    02
2    03
3    04
4    05
5    LS
Name: District, dtype: object

print (pd.to_numeric(df.District.str[-2:], errors='coerce'))
0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
5    NaN
Name: District, dtype: float64
Sign up to request clarification or add additional context in comments.

1 Comment

I add another solution, please check it.
3

You can try:

dsAttendEnroll.District=pd.to_numeric(dsAttendEnroll.District)
dsAttendEnroll.District=dsAttendEnroll.District.astype(int)

Have a look at the documentation here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.