Sione Hoghen
Sione Hoghen

Reputation: 3

ValueError: could not convert string to float: 'Y'

As i try to convert an object type column to float, I am getting ValueError: could not convert string to float: 'Y':

import pandas as pd
import numpy as np

df_train = pd.read_csv('loan_prediction/train_u6lujuX_CVtuZ9i.csv')
df_train_y = df_train.iloc[:, 12].values

df_train_y.astype(float)

Upvotes: 0

Views: 974

Answers (1)

jsmart
jsmart

Reputation: 3001

This might help you to find the non-numeric values in your data set.

First, create a data frame, and set certain elements of Column 12 to non-numeric values:

import numpy as np
import pandas as pd

nrows, ncols = (10, 15)
data = np.arange(nrows * ncols).reshape((nrows, ncols))
df = pd.DataFrame(data)

df.iloc[2:5, 12] = 'x'

Second, extract column 12, and convert to numeric type:

df_2 = df.iloc[:, 12].copy()
df_2 = pd.to_numeric(df_2, errors='coerce')

Third, find the non-numeric values (with a Boolean mask):

mask = df_2.isna()
print(df[mask].iloc[:, 12])

2    x
3    x
4    x
Name: 12, dtype: object

Upvotes: 1

Related Questions