I need to identify which columns in a dataframe are decimals and which are strings.
Using df.dtypes gives 'object' for both column types:
import pandas as pd
import decimal
data = {'dec1': [1.1, 1.2],'str1': ["a","b"]}
df = pd.DataFrame(data)
df.dec1 = df.dec1.apply(lambda x: decimal.Decimal(x))
df.dtypes
I am using the following code to know which are decimals, but there has to be a more pythonic way for something so basic. What is it?
actual_col_types = df.iloc[0].apply(type)
df_decimals = df.loc[:,actual_col_types==decimal.Decimal]