DataFrame添加datetime数据并写入.wac文件。

2024-05-12 22:05:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有一个格式正确的.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 %"]) 

现在我需要对dataframes做以下修改,但是我不知道从哪里开始,我希望我来到正确的地方寻求帮助。 对于数据格式

- 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\u format的快照以使其更清晰:

enter image description here

一旦创建了新的数据帧,例如,data\u format\u new我可以用data\u WUFI中的正确值替换'Temp gap North[C]中的值(已经具有正确的大小):

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

此时,我将在.wac文件中写入数据格式\u NEW

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

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

enter image description here

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


Tags: 文件theto数据informatnewdata