Considering the following data frame:
>>> df = pd.DataFrame(10*np.random.rand(3, 4), columns=list("ABCD")) >>> print(df) ... A B C D ... 0 8.362940 0.354027 1.916283 6.226750 ... 1 1.988232 9.003545 9.277504 8.522808 ... 2 1.141432 4.935593 2.700118 7.739108
Using a list of column names, change the type for multiple columns with applymap()
:
>>> cols = ['A', 'B'] >>> df[cols] = df[cols].applymap(np.int64) >>> print(df) ... A B C D ... 0 8 0 1.916283 6.226750 ... 1 1 9 9.277504 8.522808 ... 2 1 4 2.700118 7.739108
Or for a single column with apply()
:
>>> df['C'] = df['C'].apply(np.int64) >>> print(df) ... A B C D ... 0 8 0 1 6.226750 ... 1 1 9 9 8.522808 ... 2 1 4 2 7.739108