Johan Nilssons Lifestream

pandas.core.common.PandasError: DataFrame constructor not properly called

I am trying to receive data using mosquitto and save it as csv file using python pandas. The data is continuos until I stop the script.

import paho.mqtt.client as mqtt
import random
import schedule
import time

mqttc = mqtt.Client("python_pub")
mqttc.connect("localhost", 1883)

def job():
    mqttc.publish("hello/world", random.randint(1, 10))


while True:


import paho.mqtt.client as mqtt
import pandas as pd

def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))

def on_message(client, userdata, msg):
    datas = map(int, msg.payload)
    for num in datas:
        df = pd.DataFrame(data=datas, columns=['the_number'])

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("localhost", 1883, 60)


from above script, I get testing.csv that looks like this

    | the _number
0   | 2

2 is the last digit that I receive before I stop the script

Connected with result code 0

I was hoping to get testing.csv like this

    | the_number
0   | 3
1   | 9
2   | 5
5   | 2

To achieve that I try to change the following df = pd.DataFrame(data=datas, columns=['the_number']) to df = pd.DataFrame(data=num, columns=['the_number']) and the following error occured

pandas.core.common.PandasError: DataFrame constructor not properly called!

Do anyone have any idea how to solve the error? I also feel that I did not use the for loop properly in here.

Thank you for your suggestion and help.

via Stack Overflow

blog comments powered by Disqus
Get the source for phplifestream at Github