2017-03-07 72 views
0

我試圖使用mosquitto接收數據並使用python熊貓將其保存爲csv文件。數據是連續的,直到我停止腳本。pandas.core.common.PandasError:未正確調用DataFrame構造函數

mqtt_pub.py

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)) 

schedule.every(1).seconds.do(job) 

while True: 
    schedule.run_pending() 
    time.sleep(1) 

mqttc.loop(2) 

mqtt_sub.py

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

def on_connect(client, userdata, rc): 
    print("Connected with result code "+str(rc)) 
    client.subscribe("hello/world") 

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

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

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

client.loop_forever() 

從上面mqtt_sub.py腳本,我得到testing.csv,看起來像這樣

| the _number 
0 | 2 

2是我收到befo最後一位再我停止mqtt_sub.py腳本

Connected with result code 0 
[3] 
[9] 
[5] 
[3] 
[7] 
[2] 
... 
... 
KeyboardInterrupt 

我希望得到testing.csv這樣

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

爲了實現這個目標我試圖改變以下df = pd.DataFrame(data=datas, columns=['the_number'])df = pd.DataFrame(data=num, columns=['the_number'])並出現下列錯誤

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

有沒有人有任何想法如何解決錯誤?我也覺得我沒有在這裏正確使用for循環。

感謝您的建議和幫助。

[更新]

我添加/更改以下行中on_message方法

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

    f = open("test.csv", 'a') 
    df.to_csv(f) 
    f.close() 

與Nulljack的幫助下,我能夠得到的結果像這樣在我的CSV文件

| the_number 
0 | 3 
    | the_number 
0 | 9 
    | the_number 
0 | 5 
    | the_number 
0 | 3 
    | the_number 
0 | 7 

我的目標是在CSV文件中實現這樣的效果

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

回答

0

在我道歉之前從未使用過蚊子,如果我的理解錯誤的話。

在我看來,就像你mqtt_sub.py的ON_MESSAGE方法運行每次你mqtt_pub.py發佈的消息(即每1秒)的時間,這將導致你的testing.csv文件被覆蓋每次發佈消息

解決這個問題我會在你的on_connect方法初始化一個數據幀,然後在通過ON_MESSAGE df.append

作爲編寫到CSV你終止後的新值添加到數據幀,我不確定。

希望這有助於

+0

我相信,'on_connect'是連接時使用的方法蚊子經紀人。 'on_message'是用來顯示通過蚊子代理收到的消息的方法。 – Fang

+0

是的,所以每次你收到一條消息時都應該調用on_message方法,因此你多次調用df.to_csv(「testing.csv」)。這覆蓋以前的數據 – Nulljack

+0

http://stackoverflow.com/questions/17134942/pandas-dataframe-output-end-of-csv詳細信息如何使用熊貓而不是覆蓋文件追加到csv文件的東西@方 – Nulljack

0

其他線程裏擠滿了人,所以我在這裏提出我的反應

嘗試使用波紋管代碼

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

# Move df here 
df = pd.DataFrame(columns=['the_number']) 

def on_connect(client, userdata, rc): 
    print("Connected with result code "+str(rc)) 
    client.subscribe("hello/world") 

def on_message(client, userdata, msg): 
    datas = map(int, msg.payload) 

    # this adds the data to the dataframe at the correct index 
    df.iloc[df.size] = datas 

    # I reverted this line back to what you originally had 
    # This will overwrite the testing.csv file every time your subscriber 
    # receives a message, but since the dataframe is formatted like you want 
    # it shouldn't matter 
    df.to_csv("testing.csv") 


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

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

client.loop_forever() 
+0

該腳本返回錯誤「IndexError:單個位置索引器超出界限」 – Fang