OverflowError: Python int too large to convert to C long in Pandas

Dung Do Tien Jun 13 2021 334

Recently, while I was working with the panda’s module recently and I discovered an OverflowError: Python int too large to convert to C long. I was running a python script, where I have to convert a string column from a pandas df to int, using the astype(int) method. However, I got the error. The code was as follows:

import pandas as pd
 
df = pd.DataFrame({'t': ['123456789985', '178965423698']})
df['int'] = df['test'].astype('int')
 
print(df['int'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

How can I resolve it?

Have 1 answer(s) found.
  • H

    Huyền Trần thị Jun 13 2021

    All you need to do was pass int64 as the parameter in astype method.

    import pandas as pd
     
    data_f = pd.DataFrame({'t': ['123456789985', '178965423698']})
    data_f['int'] = data_f['test'].astype('int64')
     
    print(data_f['int'])

    I hope it helpful for you.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close