0

i want to convert incoming data to float in excel file , and i don't know how to do it , please if any one can help me i will be very appreciate

with open(file_path, "rt") as csv_file:
        content = csv.reader(csv_file)
        for row in content:
            values = []
            for column in row:
                values.append({'number_value': float(column)})
            payload = {
                'row': {'values': values}
            }

            response = prediction_client.predict(model_full_id, payload)
            print("Prediction results:")
            for result in response.payload:
                print("Predicted class name: {}".format(result.display_name))
                print("Predicted class score: {}".format(result.classification.score))

there is some data in string format, how can i convert those which is in string format to float please?

love python
  • 59
  • 2
  • 8
  • The first row in a csv file usually contains column names, and not the values. So start from the second row. – Aryerez Oct 30 '19 at 10:43
  • yes you are right , but after that it's show me this error because i have a some column in string format like 'RENTER' , the error was 'ValueError: could not convert string to float: 'RENTER'' – love python Oct 30 '19 at 11:08

2 Answers2

0

I'm not sure what the data format of your CSV file is or what you want to do with the data once it has been loaded. However, your question asks how "to convert incoming data to float in excel file".

An easy way to do this would be to use the pandas module to read read the CSV file and convert the data types within to a new dataframe.

you can use the dtype argument in the read_csv method to change the data types of the columns being read in, e.g. read an integer value as a float. Have a look at the documentation here.

In terms of your code above, you could try something like (replacing 'filename.csv' with your file name, and the 'column_name_' values with those in the CSV):

import pandas as pd
import numpy as np

df = pd.read_csv('filename.csv', dtype = {'column_name_1': np.float64, 'column_name_2': np.float64,})

You can specify which of your columns you want to import as a float type. We can the confirm this is the case by checking the data type of each column with:

print(df.dtypes)

This should then show something like:

column_name_1    float64
column_name_2    float64
dtype: object

You can then use the iterrows() function to iterate through the rows in your new dataframe. You can refer to this link for help on using this: How to iterate over rows in a DataFrame in Pandas?

Fab Dot
  • 504
  • 1
  • 5
  • 16
0

You can enclose the part of the code that generates the exception in a try block:

with open(file_path, "rt") as csv_file:
    content = csv.reader(csv_file)
    for row in content:
        values = []
        for column in row:
            try:
                values.append({'number_value': float(column)})
            except ValueError:
                values.append({'number_value': column})
        payload = {
            'row': {'values': values}
        }

        response = prediction_client.predict(model_full_id, payload)
        print("Prediction results:")
        for result in response.payload:
            print("Predicted class name: {}".format(result.display_name))
            print("Predicted class score: {}".format(result.classification.score))
Tobin
  • 2,029
  • 2
  • 11
  • 19
  • it still can't convert string to float , in fact i want to do this for all column , all should be float , it shows me this again ====>Cannot set google.protobuf.Value.number_value to 'RENTER': 'RENTER' has type , but expected one of: (, ) for field Value.number_value – love python Oct 30 '19 at 11:28
  • Can you please add an example of what an entire row/column of your csv file looks like? And if possible the entire traceback in your terminal. – Tobin Oct 30 '19 at 12:11
  • -- this is one of the rows ==>30,000,RENTER,German,N,1,25-29,German,1 3RD PL UNIT 703,CA,LONG BEACH,LOS ANGELES,90802,7.00 - 7.99,7Day,FM,0,CircAdm --and this is the error ==>TypeError: Cannot set google.protobuf.Value.number_value to 'RENTER': 'RENTER' has type , but expected one of: (, ) for field Value.number_value – love python Oct 30 '19 at 12:55
  • I am not an expert and you provided us with very little details to help us understand the problem. But from what I could understand, I see that you use the protocol buffer of google. And in this case you try to assign a string value (`RENTER`) to a property that expects a float value `google.protobuf.Value.number_value`. So try to analyze your code to see how you can harmonize your values ​​with the defined properties. – Tobin Oct 30 '19 at 15:26