-1

I created a Pandas Series like this:

txt = pd.Series(['1', '2', '3', '4', '5', ""])

print(txt)

Result:

0    1
1    2
2    3
3    4
4    5
5     
dtype: object

Find out type of each item and gives me 'str' as result.

type(txt[0])
type(txt[5])

Try to convert each item into 'int' type with .astype() but failed.

txt.astype('int')

ValueError: invalid literal for int() with base 10: ''

Try to convert each item into 'int' type with a self-defined function but got error.

txt.apply(lambda num: int(num))

ValueError: invalid literal for int() with base 10: ''

Someone please advise me how to do data type conversion in this case. My plan is to create 2 pandas Series and perform maths operation on both Series.

olinox14
  • 6,177
  • 2
  • 22
  • 39
Ong K.S
  • 229
  • 1
  • 4
  • 15

1 Answers1

1

Using to_numeric:

import pandas as pd

txt = pd.Series(['1', '2', '3', '4', '5', ""])
print(txt)
print(txt.dtypes)
txt_int = pd.to_numeric(txt, errors='coerce').astype('Int64')
print(txt_int)
print(txt_int.dtypes)

OUTPUT:

0    1
1    2
2    3
3    4
4    5
5     
dtype: object
object
0       1
1       2
2       3
3       4
4       5
5    <NA>
dtype: Int64
Int64
DirtyBit
  • 16,613
  • 4
  • 34
  • 55