前提・実現したいこと
ここに質問の内容を詳しく書いてください。
シリアル通信によりpythonで4つのセンサの情報をcsvファイルに保存することを目指しています。
4つのセンサの情報をいろんなサイトを調べてjuptyterで確認できるようになったのですが、csvファイルに書き込む前で詰まっています。
下のように出力されてしまいます。
一行目をABCDとしたいのですが、毎回センサの値を読むたびにABCDが出てきてしまいます。
プログラムのどこを変えればよいのでしょうか?
発生している問題・エラーメッセージ
A
10 972 1023 972 1023 -1.216718 2 A B C D E 30 972 1023 972 995 -1.040281 4 A B C D E 50 972 1017 973 978 0.317648 6 A B C D E 70 973 1023 972 973 -0.133971 8 A B C D E 90 972 1023 972 972 -0.819937 10 A B C D E 110 973 1023 971 991 -0.398122 12 A B C D E 130 972 1023 971 979 0.290892 14 A B C D E 150 972 1020 970 971 -0.902272 16 A B C D E 170 972 1020 973 976 0.106279 18 A B C D E 190 972 1023 972 962 -0.090337 20 21 22 23##
python
import numpy as np import matplotlib.pyplot as plt import pygame from pygame.locals import * import serial import sys import pandas as pd def main(): ser = serial.Serial("COM3") # COMポート(Arduino接続) xdegs = [0]*100 # 温度格納 ydegs = [0]*100 # 温度格納 zdegs = [0]*100 # 温度格納 wdegs = [0]*100 # 温度格納 t = np.arange(0,100,1) num = 0 while num < 10: data = ser.readline().rstrip() # \nまで読み込む(\nは削除 data = data.decode() #対話型だとこれが必要らしい (xdeg, ydeg, zdeg, wdeg) =data.split(",") a = (xdeg,ydeg, zdeg, wdeg) df = pd.DataFrame({ "A" : xdeg, "B" : ydeg, "C" : zdeg, "D" : wdeg, "E" : np.random.randn(1)}) num = num + 1 print(df) if __name__ == '__main__': main()
試したこと
import pandas as pd
import numpy as np
df = pd.DataFrame({"A" : ['foo', 'bar', 'foo', 'bar','foo', 'bar', 'foo', 'foo'],
"B" : ['one', 'one', 'two', 'three','two', 'two', 'one', 'three'],
"C" : np.random.randn(8),
"D" : np.random.randn(8)})
df
となることがわかり、このように出力した後に、CSVファイルに書き込みたいです。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/03 04:27