Python – Change the column type if the column contains characters from the Pandas

Change the column type if the column contains characters from the Pandas… here is a solution to the problem.

Change the column type if the column contains characters from the Pandas

For example, I have the value of the character “%” in some columns (non-empty objects).

 col1     col2  col3 
'4.24%' '5.22%'  8

But I want 4.24 and 5.22 as floats.

I have tried with:
for el in df.columns:
    if df[el].str.contains('%').any():
        df[el] = df[el].str.strip("%").astype(float) 

And says: AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

If I use:

if df['col1'].str.contains('%').any():
            df['col1'] = df['col1'].str.strip("%").astype(float)

Then works fine. But iterating through all columns doesn’t work.

Any help is welcome.

Solution

You need to convert to string before str.contains('%') because it also tests non-string columns:

for el in df.columns:
    if df[el].astype(str).str.contains('%').any():
        df[el] = df[el].str.strip("%").astype(float) 

print (df)
   col1  col2  col3
0  4.24  5.22     8

A better solution is to use select_ dtypes only select object columns (obviously strings):

for el in df.select_dtypes(object).columns:
    if df[el].str.contains('%').any():
        df[el] = df[el].str.strip("%").astype(float) 

Related Problems and Solutions