首页 > 解决方案 > 数据框附加日期时间数据并写入 .wac 文件

问题描述

我是 python 新手,在过去的几天里,我自学了如何使用 pandas 对存储在 txt、xls、asc 文件中的数据打开和执行操作,但是在使用数据帧进行操作时我仍然感到困惑。

我有一个格式正确的 .wac 文件(它必须随后用作软件的输入文件),但包含部分错误的值,以及一个包含正确值的 .xlsx 文件。

我已使用此代码将数据传输到两个数据帧(我使用skiprows跳过两个文件中的字符串数据):

data_format = pd.read_csv('Example.wac', skiprows=11, delim_whitespace=True, names=["Date", "Hour", "Temp gap North [C]", "RH %"])

data_WUFI =pd.read_excel('Temperature_RH_North.xlsx', skiprows=1, header=None, dtype=float, names=["Hour", "Temp gap [C]", "RH %"]) 

现在我需要对数据框进行以下修改,但我不知道从哪里开始,我希望我来对了地方寻求帮助。对于data_format

- the column 'Date' is in the format *2018-01-01* and runs to *2019-12-31*. Being obviously a date, it stays the same for 24 positions and then it increases by 1 day. I need to add rows to that column up to *2027-12-31* (without leap years)
- the column 'Hour' is in the format *01:00*. Values run from *01:00* to *24:00*. I need to add rows so that every 24 hours the date in the first column increases by one day, then the hour numbering restarts at *01:00*
- The column 'RH %' contains the same value in all rows, i.e. 0.5 

我添加了data_format的快照以使其更清晰:

在此处输入图像描述

一旦创建了新的数据框,例如data_format_NEW ,我可以用data_WUFI中的正确值(已经是正确的大小)替换“Temp gap North [C]”中的值:

data_format_NEW['Temp gap North [C]'] = data_WUFI['Temp gap [C]']

那时,我将 data_format_NEW 写入.wac文件:

data_format_NEW.to_csv('Example_NEW.wac', index=False, delim_whitespace=True)

但前 12 行必须包含字符串值,如图所示:

在此处输入图像描述

我不确定我的计划是否正确,但我希望我能够解释清楚自己

标签: pythonpandaswac

解决方案


推荐阅读