如何使用 pandas 将温度和湿度数据存储在 excel 文件中,并在一段时间后更新该数据,然后将更新的数据存储在每行中?

How to store temperature and humidity data's in excel file using pandas and update that data after some time then the updated data stored in each row?

提问人:Kuralmozhi 提问时间:7/26/2023 更新时间:7/26/2023 访问量:81

问:

我正在使用 ESP32AHT25 来测量温度和湿度数据。我需要将该数据存储到 Excel 文件中。我正在使用 pandas 库将此数据存储在 csv 文件中,我的代码是,

    pd_data = pd.DataFrame(columns=['ID','Temperature(°C)','Humidity(%)'])

    while True:
        pd_data = pd_data._append({'ID':id_json,'Temperature(°C)':temp,'Humidity(%)':humi},
                                  ignore_index=True)
        pd_data.to_csv('my_file.csv', index=False)
        time.sleep(5)

我想在 5 秒后存储温度和湿度数据,此过程在每行 5 秒后将数据连续存储在 csv 文件中。但我不知道该怎么做。谁能帮我?

我正在使用 pandas 库,如果有任何其他技术可以存储在 excel 表格中,那么也建议我这样做。我将尝试。

python-3.x 熊猫 python-2.7 esp32

评论

0赞 JackColo_Ben4 7/26/2023
从纸面上看,你的代码草稿是可以的(除了没有带下划线的_append!),并且你迟早需要一种方法来退出那个“while True”。你的问题到底是什么?
0赞 Kuralmozhi 7/26/2023
stored_data我想要这种输出
0赞 JackColo_Ben4 7/26/2023
理解。但目前还不完全清楚......您是否已经将来自传感器的所有数据存储在.json文件中?或者您想在每次发现新的 temp 和 humi 值时更新数据帧以及数据提取?
0赞 Kuralmozhi 7/26/2023
不。我想在每次找到新的温度和湿度值时更新数据帧以及数据提取

答:

1赞 JackColo_Ben4 7/26/2023 #1

好的,这里有一个解决方案,您可以将其用作实现目标的起点。
如果有什么不清楚的地方,请告诉我。

  1. 下次,请尝试发布您自己的解决方案(即使它不起作用),并附上您的想法和更多信息。
  2. 避免使用不准确的 time.sleep(),最重要的是,它会阻止程序的执行。
  3. 与其检索数据并将它们永远存储在.csv中,直到发生
    外部中断(=> while True 语句),不如将最大时间间隔或最大扫描次数固定为限制。
  4. 第一次只创建 .csv 文件,以后使用时只更新同一个文件会更方便。
  5. 您可以将 #** 指示的行替换为 #*** 以避免警告:“FutureWarning:frame.append 方法已弃用,将在将来的版本中从 pandas 中删除。请改用 pandas.concat。
import time
import random
import os.path
import datetime
import pandas as pd 

csv_file = 'my_file.csv'

# Create temporary dataframe to store new readings 
temp_df = pd.DataFrame(columns=['Timestamp', 'Temperature(°C)', 'Humidity(%)'])

# Check if "my_file.csv" exists (it should be placed in the same directory)
if not os.path.isfile(csv_file):
    already = False
else:
    already = True

# Set the time limit in seconds
loop_duration = 20 
# Set the starting times 
start_time_extern = time.time()              #as float
start_time_inside = datetime.datetime.now()  #as datetime object

first_round = True
# Until the loop_duration expires
while (time.time() - start_time_extern) < loop_duration:
     current_time = datetime.datetime.now()
     elapsed_time = (current_time - start_time_inside).total_seconds()
     
     if first_round or elapsed_time >= 5:
        """ Now! and every 5 seconds: 
            _Get your data from sensors someway;
            _Do your additional operations on readings to create "temp" and "humi";
            _Add sensors' ouputs + timestamp to csv.
        N.B. Here, I've used random variables as placeholders (with 2f precision)
        N.B.2. Change to "if elapsed_time >= 5:" to wait 5 sec even the first time)
        """
        temperature = random.uniform(-10, 40)
        # Reduce the digits after the decimal point to 2
        temperature_ok = round(temperature * 100) / 100    

        humidity_percent = random.uniform(0, 100)
        humidity = humidity_percent / 100
        # Round digits as above
        humidity_ok = round(humidity * 100) / 100

        # Create current timestamp 
        timestamp = current_time.strftime('%Y-%m-%d %H:%M:%S')
        # Append to the temporary dataframe
        #**
        temp_df = temp_df.append({'Timestamp': timestamp, 
                                  'Temperature(°C)': temperature_ok, 
                                  'Humidity(%)': humidity_ok}, 
                                  ignore_index=True) 
        #***        
        #temp_df = pd.concat([temp_df, pd.DataFrame.from_records([{'Timestamp': timestamp,  
        #'Temperature(°C)': temperature_ok, 'Humidity(%)': humidity_ok}])],
        #ignore_index=True)  

        if already:
            """ If "my_file.csv" already exists...
            - Copy to csv appending data to the end of the file (mode='a' param)
              rather than overwriting any existing data.
            - Specify that the file does not add the header row with the names 
              of colums (header=False param).
            - State that the dataframe's index is not be included 
              (index=False parameter).
            """
            temp_df.to_csv(csv_file, mode='a', header=False, index=False)
            # Empty the df, to avoid to insert int the csv duplicate rows 
            temp_df = pd.DataFrame(columns=['Timestamp', 'Temperature(°C)', 'Humidity(%)'])
        else: 
            # Copy to csv including headers
            temp_df.to_csv(csv_file, index=False)
        
        # Set the starting time to now, to count again for 5 seconds 
        start_time_inside = current_time 

        first_round = False

CSV格式

Timestamp,Temperature(°C),Humidity(%)
2023-07-26 16:47:42,10.54,0.23
2023-07-26 16:47:47,28.19,0.71
2023-07-26 16:47:52,20.14,0.8
2023-07-26 16:47:57,-9.6,0.9
2023-07-26 16:48:10,-5.33,0.91
2023-07-26 16:48:15,21.86,0.32
2023-07-26 16:48:20,14.44,0.68
2023-07-26 16:48:25,24.9,0.59

评论

0赞 Kuralmozhi 7/27/2023
谢谢@Myron_Ben4。它可以工作,但我只有 5 个数据。如何连续获取数据?
0赞 JackColo_Ben4 7/27/2023
增加loop_duration,在我的示例中只有 20 秒......或使用“while True”。
0赞 Kuralmozhi 7/27/2023
还行。非常感谢@Myron_Ben4