[英]pandas.core.common.PandasError: DataFrame constructor not properly called
我正在嘗試使用mosquitto接收數據,並使用python pandas將其另存為csv文件。 數據是連續的,直到我停止腳本為止。
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
是我停止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循環。
感謝您的建議和幫助。
[UPDATE]
我在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
如果我的理解是錯誤的,在向我道歉之前從未使用過蚊子。
在我看來,每次mqtt_pub.py發布消息(例如,每隔一秒鍾)時,都會運行mqtt_sub.py中的on_message方法,這將導致您每次發布消息時覆蓋您的testing.csv文件
為了解決這個問題,我會在您的on_connect方法中初始化一個數據框,然后在on_message中通過df.append將新值添加到該數據框
至於終止后寫到csv,我不確定。
希望這可以幫助
其他線程很擁擠,所以我在這里移動了我的回復
嘗試使用以下代碼
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.