anayisse2
anayisse2

Reputation: 103

Error ::: ValueError: could not convert string to float: '28,37'

I tried to fit a logistic regression on my data but i got this error ValueError: could not convert string to float: '28,37' My code:

 X_train, X_test, y_train, y_test = train_test_split(X, y,
                                            stratify=y, 
                                            test_size=0.3)
 logisticRegr = LogisticRegression()
 logisticRegr.fit(X_train, y_train)
 predictions = logisticRegr.predict(X_test)

How can i solve it please.?

Upvotes: 2

Views: 1082

Answers (3)

Hasitha Amarathunga
Hasitha Amarathunga

Reputation: 2005

The problem is ',' inside of string because you cannot convert string like "28,37" to float. So the string should like "28.37"

print(float("28.37"))

28.37

Upvotes: 0

prosti
prosti

Reputation: 46351

How about?

df.column1=df.column1.str.replace(',', '.', regex=False)
df.column1 = df.column1.astype(float)

After I tested

  column0      column1
0    row0     179319,0
1    row1      89659,5
2    row2      59773.0
3    row3     44829,75
4    row4      35863.8
5    row5      29886.5
6    row6      25617,0
7    row7    22414.875
8    row8  19924.33333
  column0      column1
0    row0     179319.0
1    row1      89659.5
2    row2      59773.0
3    row3     44829.75
4    row4      35863.8
5    row5      29886.5
6    row6      25617.0
7    row7    22414.875
8    row8  19924.33333

Upvotes: 1

Mike Scotty
Mike Scotty

Reputation: 10782

You have the wrong separator in the string. You should use a . instead of a ,.

Compare:

print(float('28,37'))

ValueError: could not convert string to float: '28,37'

To:

print(float('28.37'))

28.37

Upvotes: 2

Related Questions