0

I have a script that queries data from a database. It then exports it to a CSV file. The goal is to create a plot with the numeric data in the column. But the script does not see any numerical values in the column specified.

import pandas.io.data
import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame
import datetime
import pandas.io.data
import psycopg2
import csv
import pandas.io.data

conn = psycopg2.connect("host='172.31.98.161' dbname='servers' user='seeker'")
cur = conn.cursor() #Create the cursor
cur.execute("select hostname, used_ram_raw from server_perf  where                 hostname='localhost.localdomain'")
rows = cur.fetchall()
conn.close()

df = pd.DataFrame(rows, columns=['One','Two'])
df.to_csv("file.csv")
df = df.convert_objects(convert_numeric=True)    
df[['Two']].plot()
plt.show()

The above script produces the following file called "file.csv"

,One,Two
0,localhost.localdomain,1521819648.00
1,localhost.localdomain,1632538624.00
2,localhost.localdomain,1633038336.00
3,localhost.localdomain,1632264192.00

The error produced is whn trying to plot is:

TypeError: Empty 'DataFrame': no numeric data to plot

Just for testing I did a

  print df[['Two']]

and it printed the following

             Two
0  1521819648.00
1  1632538624.00
2  1633038336.00
3  1632264192.00

What am I doing wrong? :(

1 Answer 1

3

It's very likely that if you would type

df.Two.dtype

It would output that its type is a string (It would say it's Object or something like that).

This is easily remedied. If you need to do something numeric with it, use astype:

df.Two.astype(float)

In fact, you can just reassign it back to itself:

df.Two = df.Two.astype(float)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Ami !!! It worked. Here is the corrected code. df = pd.DataFrame(rows, columns=['One','Two']) df.to_csv("file.csv") print df[['Two']] print df.Two.dtype df.Two = df.Two.astype(float) df[['Two']].plot() plt.show()
I had to do this for the x-axis and y-axis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.