max
max

Reputation: 693

ValueError: Must pass DataFrame with boolean values only - When converting Pandas Columns to Numeric

I'm trying to convert these columns to numeric but I get this error, and haven't found much of anything on it for this specific use case on Google or Stack Overflow:

ValueError: Must pass DataFrame with boolean values only

df:

    5   6   7   8   9   10  11
0   0   0   7   0   0   0   11
1   5   0   0   0   0   0   0
2   0   0   0   0   9   0   0

#dtypes:
0                          object
1                          object
2                          object
5                          object
6                          object

cols = df.iloc[:,0:]

df[cols] = df[cols].apply(pd.to_numeric, errors='ignore', axis=1).fillna(df)

#I've tried with errors = 'coerce' and without fillna(df)

What can I try next?

Upvotes: 0

Views: 229

Answers (1)

Aniket Bote
Aniket Bote

Reputation: 3564

If you want to convert all the columns to numeric you can use

df = df.astype(int)

If you want to convert specific columns to numeric you can pass the dictionary in astype.

convert_dict = {'A': int, 
                'C': float
               } 
  
df = df.astype(convert_dict)

Upvotes: 1

Related Questions