Explanation of my issue
Take this DataFrame for example:
pd.DataFrame(
data = np.array([
['A','1'],
['B','2'],
['C', 'False'],
])
)
Is there a good way to appropriately set the second column's element type to either float or boolean?
I am simply given that DataFrame where all variables are initially strings. In reality, I have tons of rows and each DataFrame is different so the indices that need to be set to floats and bools change. Therefore, I cannot create a default dtype 'template' to refer to.
Solutions I've Explored
- pandas does have the
df.to_numeric()
function but you can only set non floats to Nans by settingerrors='coerce'
so this doesn't work. Similar issue with thedf.astype()
function - I could loop through each index try casting types till one sticks but this isn't elegant so I feel like there's a better way
Summary
Essentially, given a series object where elements are initially of type string, I need to cast the appropriate elements to either type float or bool. Is there an elegant way of doing this without looping through each element and casting either float or bool? Is there a pandas function that I'm missing?
Thanks in advance for any help!