Why is df.corr() giving “ValueError: could not convert string to float” ?
Share
Sign up to our innovative Q&A platform to pose your queries, share your wisdom, and engage with a community of inquisitive minds.
Log in to our dynamic platform to ask insightful questions, provide valuable answers, and connect with a vibrant community of curious minds.
Forgot your password? No worries, we're here to help! Simply enter your email address, and we'll send you a link. Click the link, and you'll receive another email with a temporary password. Use that password to log in and set up your new one!
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The ValueError: could not convert string to float when calling df.corr() typically occurs because the method is trying to compute correlations on columns that contain non-numeric data, such as strings or mixed types. Since correlation computations require numerical data, the presence of strings in yRead more
The
ValueError: could not convert string to float
when callingdf.corr()
typically occurs because the method is trying to compute correlations on columns that contain non-numeric data, such as strings or mixed types. Since correlation computations require numerical data, the presence of strings in your DataFrame causes this error.Here are a few steps to troubleshoot and resolve the issue:
- Check for Non-Numeric Columns: Ensure that all columns used in
- Exclude Non-Numeric Columns: Use
- Convert or Handle Non-Numeric Data: If some columns contain numeric data stored as strings, convert them to appropriate numeric types using
- Identify Problematic Columns: If the issue persists, identify the specific columns causing the error:
See lessdf.corr()
contain numeric data types. You can check the data types of each column usingdf.dtypes
.df.select_dtypes(include=[np.number])
to select only numeric columns before applyingdf.corr()
:pd.to_numeric
:This converts the values and sets non-convertible values to
To get rid of this error use: numeric_only=True df.corr(numeric_only=True) This is ignoring the columns that are 'object' type while calculating correlation.
To get rid of this error use: numeric_only=True
This is ignoring the columns that are ‘object’ type while calculating correlation.
See less