简体   繁体   中英

How to convert the last several columns to integer from string type in pandas

I have a df called df .

I want to convert the last 10 columns of this dataframe from string type to integers. How can I do this the pythonic way?

I think the fastest method is to use convert_objects and select the last 10 columns using subscript/slicing notation, example:

In [23]:

df = pd.DataFrame({'a':['1','2','3','4','5']})

df = pd.concat([df]*11, axis=1)
df.columns = list('abcdefghijk')
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 11 columns):
a    5 non-null object
b    5 non-null object
c    5 non-null object
d    5 non-null object
e    5 non-null object
f    5 non-null object
g    5 non-null object
h    5 non-null object
i    5 non-null object
j    5 non-null object
k    5 non-null object
dtypes: object(11)
memory usage: 480.0+ bytes
In [21]:

converted = df[df.columns[-10:]].convert_objects(convert_numeric=True)
converted
Out[21]:
   b  c  d  e  f  g  h  i  j  k
0  1  1  1  1  1  1  1  1  1  1
1  2  2  2  2  2  2  2  2  2  2
2  3  3  3  3  3  3  3  3  3  3
3  4  4  4  4  4  4  4  4  4  4
4  5  5  5  5  5  5  5  5  5  5
In [22]:

converted.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 10 columns):
b    5 non-null int64
c    5 non-null int64
d    5 non-null int64
e    5 non-null int64
f    5 non-null int64
g    5 non-null int64
h    5 non-null int64
i    5 non-null int64
j    5 non-null int64
k    5 non-null int64
dtypes: int64(10)
memory usage: 440.0 bytes

You can then either directly assign the result back:

In [31]:

df[df.columns[-10:]] = converted
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 11 columns):
a    5 non-null object
b    5 non-null int64
c    5 non-null int64
d    5 non-null int64
e    5 non-null int64
f    5 non-null int64
g    5 non-null int64
h    5 non-null int64
i    5 non-null int64
j    5 non-null int64
k    5 non-null int64
dtypes: int64(10), object(1)
memory usage: 480.0+ bytes

or do it in a 1 liner:

In [33]:

df[df.columns[-10:]] = df[df.columns[-10:]].convert_objects(convert_numeric=True)
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns (total 11 columns):
a    5 non-null object
b    5 non-null int64
c    5 non-null int64
d    5 non-null int64
e    5 non-null int64
f    5 non-null int64
g    5 non-null int64
h    5 non-null int64
i    5 non-null int64
j    5 non-null int64
k    5 non-null int64
dtypes: int64(10), object(1)
memory usage: 480.0+ bytes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM