I am creating the columns for an empty dataframe and need to set a particular column (Room #) to float:
columns = ['Time','Name','Company','Room #', 'etc'] # all type object
df["Room #"] = (df["Room #"]).apply(pd.to_numeric, errors='coerce')
However when I check the types at this point, it is int64 instead of the desired float:
df.infer_objects().dtypes
Time object
Name object
Company object
Room # int64
etc object
I don't want to create this as a dictionary because the actual data is fed to the frame later. What is the correct method to coerce the Room # into a float type for that column only?
Use astype
:
columns = ['Time','Name','Company','Room #', 'etc']
df = pd.DataFrame(columns=columns).astype({'Room #': float})
print(df.dtypes)
Output:
Time object
Name object
Company object
Room # float64
etc object
dtype: object
Or, using your method, force the dtype with downcast='float'
:
columns = ['Time','Name','Company','Room #', 'etc']
df = pd.DataFrame(columns=columns)
df['Room #'] = pd.to_numeric(df['Room #'], errors='coerce', downcast='float')
print(df.dtypes)
Output:
Time object
Name object
Company object
Room # float32
etc object
dtype: object