Changing datatype of multiple columns by range
You can access columns by index (position).
df.iloc[:,16:42] = df.iloc[:,16:42].apply(pd.to_numeric)
I know I can change the data type of columns by passing the column names, for example
df = df.astype({'col1': 'object', 'col2': 'int'})
but what if I want to change multiple columns by a given range? My df contains 50+ columns so I don't want to change them all by name. I want to set columns 17 to 41 as ints and I've tried a few variations, for example:
df = df.astype([17:41], 'int64')
but can't get the syntax to work. Any ideas?