Pandas: Convert Dtype 'Object' to Int

Pandas: convert dtype 'object' to int

Documenting the answer that worked for me based on the comment by @piRSquared.

I needed to convert to a string first, then an integer.

>>> df['purchase'].astype(str).astype(int)

Converting object to Int pandas

You need assign output back:

#maybe also works omit astype(str)
data3['Title'] = data3['Title'].astype(str).astype(int)

Or:

data3['Title'] = pd.to_numeric(data3['Title'])

Sample:

data3 = pd.DataFrame({'Title':['15','12','10']})
print (data3)
Title
0 15
1 12
2 10

print (data3.dtypes)
Title object
dtype: object

data3['Title'] = pd.to_numeric(data3['Title'])
print (data3.dtypes)
Title int64
dtype: object

data3['Title'] = data3['Title'].astype(int)

print (data3.dtypes)
Title int32
dtype: object

Convert dtype from object to int

Given the invalid literal error, the conversion from str to int fails as you have decimal commas in your numbers. These are nice for readability but throw a wrench into the conversion to integer. Deleting the commas does the trick.

In particular, use

df['enrollment'] = df['enrollment'].str.replace(',', '').astype(int)

Pandas: convert dtype 'object' to int

Documenting the answer that worked for me based on the comment by @piRSquared.

I needed to convert to a string first, then an integer.

>>> df['purchase'].astype(str).astype(int)

how to convert object to int or float in pandas

Try for example:

df['X1'] = df['X1'].astype(str).astype(int)

If you want to format all columns try:

df = df.astype(int)

This is because, when you import a .csv file, most of the columns are transformed into objects.

converting object to int of a large dataframe

Try cast to int64:

df['user'] = df['user'].astype(np.int64)

Or:

df['user'] = df['user'].astype('int64')

print (df['user'])
0 1101110110100
1 1111222555555
2 1112223365556
3 1113656560005
Name: user, dtype: int64

EDIT:

#convert not parseable values to NaNs
df['user'] = pd.to_numeric(df['user'], errors='coerce')
#remove rows with NaNs
df = df.dropna('user')
df['user'] = df['user'].astype(np.int64)

Or:

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

converting object to int type in pandas dataframe

df['Total Bytes'] = df['Total Bytes'].apply(lambda x: x.replace(',',''))
df['Total Bytes'] = pd.to_numeric(df['Total Bytes'])

Converting dtype: object to integer for a column that has numbers with spaces between them

One option is to apply .str.split() first in order to split by whitespaces(even if the anyone of them has more than one character length), then concatenate (''.join()) while removing those whitespaces along with converting to integers(int()) such as

j=0
for i in df['value'].str.split():
df['value'][j]=int(''.join(i))
j+=1


Related Topics



Leave a reply



Submit