Visit Our UNIX and Linux User Community


Python : Problem with " TypeError: float() argument must be a string or a number "


 
Thread Tools Search this Thread
Top Forums Programming Python : Problem with " TypeError: float() argument must be a string or a number "
# 1  
Old 01-18-2020
Python : Problem with " TypeError: float() argument must be a string or a number "

Hello !



I'm creating a CGI which allow to display graph from some data.



The datas looks like :
Code:

2020-01-13-00-00,384.00,350.00
2020-01-13-06-00,384.00,350.00
2020-01-13-12-00,384.00,350.00
2020-01-13-18-00,384.00,350.00
2020-01-14-00-00,384.00,350.00
2020-01-14-06-00,384.00,350.00
2020-01-14-12-00,384.00,350.00

I use the ipywidgets, pandas and plotly.express librarys in order to create a web page which will display a graph with two buttons : the first to display the graph and the second to display the graph with the trendline.



My script is :



Code:
import ipywidgets as widgets
import pandas as pd
import plotly.express as px
import os


 
####### Button 1 ########


 
button = widgets.Button(description="graph")
display(button)


 
def graph():
        df = pd.read_csv('/xxx/xxx/xxx/Test.txt')
        df.head()
        fig = px.line(df, x = 'Date', y ='Total Used', title='DF command graph')
        fig.update_traces(mode='markers')
        fig.show();
 


output = widgets.Output()
 


@output.capture()
def on_button_clicked(b):
        graph();


 
button.on_click(on_button_clicked)
display(output)




####### Button 2 #########
 


button2 = widgets.Button(description='trend')
display(button2)
 


def trend():
        df = pd.read_csv('/xxx/xxx/xxx/Test.txt')
        df.head()
        fig = px.scatter(float(df), x="Date", y="Total Used", trendline="ols")
        fig.show()
 


output2 = widgets.Output()
 


@output2.capture()
def on_button2_clicked(b):
        trend()
 


button2.on_click(on_button2_clicked)
display(output2)
 


graph()

The first step ( displaying the grap ) works prefectly :

Image


But when I press the trend button, I've this error :

Code:
--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/.local/lib/python3.6/site-packages/ipywidgets/widgets/widget_output.py in inner(*args, **kwargs)
    101                     self.clear_output(*clear_args, **clear_kwargs)
    102                 with self:
--> 103                     return func(*args, **kwargs)
    104             return inner
    105         return capture_decorator


<ipython-input-17-8774359d7459> in on_button2_clicked(b)
     41 @output2.capture()
     42 def on_button2_clicked(b):
---> 43     trend()
     44 
     45 button2.on_click(on_button2_clicked)


<ipython-input-17-8774359d7459> in trend()
     34     df = pd.read_csv('/home/timothee/Desktop/Test.txt')
     35     df.head()
---> 36     fig = px.scatter(float(df), x="Date", y="Total Used", trendline="ols")
     37     fig.show()
     38 


TypeError: float() argument must be a string or a number, not 'DataFrame'

I know that in this situation, the trendline make no sense, bujt it's just an example.


Could you show me how to fix this error ?

Last edited by Scrutinizer; 01-18-2020 at 06:23 AM.. Reason: quote tags -> code tags for output sample
# 2  
Old 01-18-2020
According to : Pandas' api reference

pandas.read_csv returns
Code:
DataFrame or TextParser
A comma-separated values (csv) file is returned as two-dimensional data structure with labeled axes.

Which is not the string or number, that float() expects

Previous Thread | Next Thread
Test Your Knowledge in Computers #881
Difficulty: Medium
One many threat vectors to a modern SCADA system is the threat of unauthorized access to the control software,
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

2. SuSE

"ssh suse-server 'python -V' > python-version.out" not redirecting

Okay, so I have had this problem on openSUSE, and Debian systems now and I am hoping for a little help. I think it has something to do with Python but I couldn't find a proper Python area here. I am trying to redirect the output of "ssh suse-server 'python -V'" to a file. It seems that no matter... (3 Replies)
Discussion started by: Druonysus
3 Replies

3. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. HP-UX

Script to identify the line number containing a particular "string"

Hi, I have a log file, where i am required to identify the line number, where a particular string/line appears in the log file. And then copy 200 lines above that line number to a new file. Can someone provide pointers on how to write this script or what command to be used ? Any... (1 Reply)
Discussion started by: kk2202
1 Replies

6. Shell Programming and Scripting

Shell Script to identify the line number containing a particular "string"

Hi, I have a log file, where i am required to identify the line number, where a particular string/line appears in the log file. And then copy 200 lines above that line number to a new file. Can someone provide pointers on how to write this script or what command to be used ? Any... (2 Replies)
Discussion started by: kk2202
2 Replies

7. Shell Programming and Scripting

input string="3MMTQSZ348GGMZRQWMJM4SD6M";output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6

input string="3MMTQSZ348GGMZRQWMJM4SD6M" output string="3MMTQ-SZ348-GGMZR-QWMJM-4SD6M" using linux shell script (4 Replies)
Discussion started by: pankajd
4 Replies

8. UNIX for Advanced & Expert Users

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have a problem about the Oracle related components. I'm not able to find any answer yet, and waiting for your responses... Here is the configuration of my system: * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or... (1 Reply)
Discussion started by: talipk
1 Replies

9. UNIX and Linux Applications

A question/problem about oracle "tns listener" and "enterprise manager"

hi, I have * an IBM P550 machine, * an AIX 5.3 running on it and * an oracle database, already installed on it. The problem (or question of my own) is: Oracle tns listener, "CT_LISTENER", and the enterprise manager (EM) of the instance, which is uniq instance and called... (0 Replies)
Discussion started by: talipk
0 Replies

10. UNIX for Dummies Questions & Answers

Problem: Need to check if a string argument contains "-"

I am passing a string as argument. Need to check if it contains "-". If it contains "-" then check if it contains "-r" .If Yes then print some message else check if it contains "-t".If yes print some message. How this check can be done using shell script? How I can do this by using IF OR... (7 Replies)
Discussion started by: nehagupta2008
7 Replies

Featured Tech Videos