On a dataframe with >100 columns I want pandas (v1.4.2) to automatically convert all columns to the "best" dtype. According to the docs df.convert_dtypes() or df.infer_objects() should do the trick. Consider the following example:
>>df = pd.DataFrame({"A":["1","2"], "C":["abc","bcd"]})
>>df
A C
0 1 abc
1 2 bcd
>>df.dtypes
A object
C object
dtype: object
>>df.convert_dtypes().dtypes
A string
C string
dtype: object
>>df.infer_objects().dtypes
A object
C object
dtype: object
Why is column A
not converted into int
? What would be an alternative if I am trying the wrong pandas methods?
CodePudding user response:
To convert A into int you can use:
df['A'] = pd.to_numeric(df['A'])
CodePudding user response:
To make it an int
use:
df['A'] = df['A'].astype('int')