Pandas Convert String to Int

Pandas convert string to int

You need add parameter errors='coerce' to function to_numeric:

ID = pd.to_numeric(ID, errors='coerce')

If ID is column:

df.ID = pd.to_numeric(df.ID, errors='coerce')

but non numeric are converted to NaN, so all values are float.

For int need convert NaN to some value e.g. 0 and then cast to int:

df.ID = pd.to_numeric(df.ID, errors='coerce').fillna(0).astype(np.int64)

Sample:

df = pd.DataFrame({'ID':['4806105017087','4806105017087','CN414149']})
print (df)
ID
0 4806105017087
1 4806105017087
2 CN414149

print (pd.to_numeric(df.ID, errors='coerce'))
0 4.806105e+12
1 4.806105e+12
2 NaN
Name: ID, dtype: float64

df.ID = pd.to_numeric(df.ID, errors='coerce').fillna(0).astype(np.int64)
print (df)
ID
0 4806105017087
1 4806105017087
2 0

EDIT: If use pandas 0.25+ then is possible use integer_na:

df.ID = pd.to_numeric(df.ID, errors='coerce').astype('Int64')
print (df)
ID
0 4806105017087
1 4806105017087
2 NaN

How to convert multiple columns from string to integer in pandas dataframe?

Try with replace():

df_all['1981'] = df_all['1981'].replace(',','',regex=True)

Now try with astype() method:

df_all['1981'] = df_all['1981'].astype('int64')

If you want to convert multiple columns then:

df[df.columns[2:]]=df[df.columns[2:]].replace(',','',regex=True).astype('int64')

Pandas how to convert time in string to integer?

Use pd.to_timedelta(df['time']).dt.total_seconds().

Demo:

>>> df = pd.DataFrame({'time': ['00:04:01.2540000', '00:02:17.6700000']})
>>> df
time
0 00:04:01.2540000
1 00:02:17.6700000
>>> pd.to_timedelta(df['time']).dt.total_seconds()
0 241.254
1 137.670
Name: time, dtype: float64

edit: chain an .astype(int) if you want to truncate the decimal places.

Python Pandas Convert String to int/float

Just use the string methods to get only the numbers that matter. There are a lot of options available depending upon how messy or formatted your column is:

import pandas as pd

df['amount'] = pd.to_numeric(df.amount.str.replace('hours', ''), downcast='integer')
# or
df['amount'] = pd.to_numeric(df.amount.str[:-5], downcast='integer')
# or
df['amount'] = pd.to_numeric(df.amount.str.extract('(\d+\.?\d*)')[0], downcast='integer')

All output:

            day  amount
2018-08-23 3 24
2018-08-24 4 8
2018-08-25 5 32
2018-08-26 6 24
2018-08-27 0 24

How to convert a column of a dataframe that has both string and int to only int in python

Use replace() and astype() method:

df['weight']=df['weight'].replace('kg','',regex=True).astype(float)

OR

Another way is by using string slicing and astype() method:

df['weight']=df['weight'].str[:-2].astype(float)

Converting string to int in Pandas column

You need to call pd.numeric like this:

import pandas as pd

df = pd.DataFrame(data=[str(i + 1980) for i in range(10)], columns=['Congress'])
df['Congress'] = pd.to_numeric(df['Congress'], errors='ignore')
print(df)

The code above is meant as a toy example, you just need to change your line:

df['Congress'] = df['Congress'].pd.to_numeric(errors='ignore')

to:

df['Congress'] = pd.to_numeric(df['Congress'], errors='ignore')

Error when converting string to integer when values are numbers in Pandas data frame

Looks like there is a "A415" value in your column. Could be a typo?

You can check if this is the case by getting a list of the unique values in this pandas column, like below. This is a quick way of knowing if all values look alright.

df['Area Code'].unique()


Related Topics



Leave a reply



Submit