023_py_pi_pandas - kotaproj/study_zenpan GitHub Wiki

ラズパイでpandasがインポートできない

(env_twilite) pi@raspberrypi11:~/study_twilite $ python study_csv.py 
Traceback (most recent call last):
  File "study_csv.py", line 1, in <module>
    import pandas as pd
  File "/home/pi/study_twilite/env_twilite/lib/python3.7/site-packages/pandas/__init__.py", line 17, in <module>
    "Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
ImportError: Unable to import required dependencies:
numpy: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.7 from "/home/pi/study_twilite/env_twilite/bin/python"
  * The NumPy version is: "1.19.4"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: libf77blas.so.3: cannot open shared object file: No such file or directory
  110  sudo apt-get install python-pandas
  111  python study_csv.py 
  112  pip install pandas
  113  python study_csv.py 
  114  sudo apt install libatlas-base-dev
  115  python study_csv.py 

pandasでcsvファイルを追記

  • ポイント
    • to_csv()にて、mode='a'を追加
    • ファイルが存在する場合は、header=False
import pandas as pd
import os
from datetime import datetime

ENVTEMP_CSV = "env_temp.csv"
SENSOR_NAME = "twilite"

def add_envcsv(rdata, sensor_name=SENSOR_NAME, csv_file=ENVTEMP_CSV):

    temp = str(int(rdata[53:(53+4)], 16) / 100)
    hum = str(int(rdata[57:(57+4)], 16) / 100)
    pressure = str(int(rdata[61:(61+4)], 16))

    l = [sensor_name, datetime.now(), temp, hum, pressure]

    # header no umu
    hd_flg = False if os.path.isfile(ENVTEMP_CSV) else True
    df = pd.DataFrame([l], columns=['date', 'sensor', 'temp', 'hum', 'pressure'])

    df.to_csv(csv_file, index=False, encoding="utf-8", mode='a', header=hd_flg)
    return


if __name__=='__main__':

    line = b'RAW PACKET -> :0101810B717A0000000000534253310000000008C10C2803E40CDC046B33'
    add_envcsv(line)

グラフの描画

https://note.nkmk.me/python-pandas-plot/

Series関連

リストからSeriesを作成

import pandas as pd

sample_list = ['a', 'b', 'c', 'd']
series = pd.Series(sample_list)
series

dfにseriesを追加

df["koko"] = series

その要素があるか確認する

df_r = pd.read_csv(csv_file)
df_r

<output>
	date	episode
0	2021_0123	10
1	2021_0123	12

---

10 == df_r["episode"]
<output>
0     True
1    False
Name: episode, dtype: bool

---

10 in df_r["episode"].values

<output>
True

⚠️ **GitHub.com Fallback** ⚠️