Suppose I have a column that contains object which contains float(like this "12.45"). How can I check if this column exists in panda data frame. I want to find this column first and convert all of it's values into float. In another words how can I look for the columns that have objects containing numeric values like floats "12.54". I do not want to look for the objects that contain non numeric values like "cat" or "dog"
for example:
import pandas as pd
df = pd.DataFrame({"column1":[ "12.44", "56.78", "45.87"],
"column2":["cat", "dog", "horse"]})
I want to check if column1 exists in my columns in order to convert all of it's values to float
CodePudding user response:
You can try to convert column by column and catch errors along the way. Columns that won't convert are unchanged because the exception is raised before the column is reassigned.
import pandas as pd
df = pd.DataFrame({"column1":[ "12.44", "56.78", "45.87"],
"column2":["cat", "dog", "horse"],
"column3":["1", "2", "3"]})
for colname in df.columns:
try:
df[colname] = df[colname].astype('float')
print(f"{colname} converted")
except ValueError:
print(f"{colname} failed")
print(df.dtypes)
CodePudding user response:
You can use pd.to_numeric
to try to convert the strings to numeric values. Then, check for each element in the converted columns whether it is an instance of float type by using .applymap()
and isinstance(x, float)
. Finally, check for any column value in a column is of type float
by .any()
:
df.apply(pd.to_numeric, errors="ignore").applymap(lambda x: isinstance(x, float)).any()
Result:
column1 True
column2 False
dtype: bool
True
value of column1
corresponds to it has at least one element of type float