我有这个df:
df = pd.DataFrame({"Time": ["2020-04-09 06:40:40.559719","2020-04-09 06:40:40.559719", 'NaT', "2020-04-09 06:40:40.559719", 'NaT', 'NaT', 'NaT', '2020-04-09 16:50:38.559871', 'NaT', '2020-04-29 16:50:38.559871'],
"Power": [7500, 6000, 'NaN', 6000, 'NaN', 'NaN', 'NaN', 3600, 'NaN', 4200],
"Total Energy": [5000, 5100, 'NaN', 5300, 'NaN', 'NaN', 'NaN', 5360, 'NaN', 5500],
"ID": [1, 1, 'NaN', 1, 'NaN', 'NaN', 'NaN', 2, 'NaN', 2],
"Energy": [500, 600, 'NaN', 800, 'NaN', 'NaN', 'NaN', 60, 'NaN', 200]},
index=pd.date_range(start = "2020-04-09 6:45", periods = 10, freq = 'T'))
df['Time'] = pd.to_datetime(df['Time'])
df['Power'] = pd.to_numeric(df['Power'], errors = 'coerce')
df['Total Energy'] = pd.to_numeric(df['Total Energy'], errors = 'coerce')
df['ID'] = pd.to_numeric(df['ID'], errors = 'coerce')
df['Energy'] = pd.to_numeric(df['Energy'], errors = 'coerce')
df
输出:
Time Power Total Energy ID Energy
2020-04-09 06:45:00 2020-04-09 06:40:40.559719 7500.0 5000.0 1.0 500.0
2020-04-09 06:46:00 2020-04-09 06:40:40.559719 6000.0 5100.0 1.0 600.0
2020-04-09 06:47:00 NaT NaN NaN NaN NaN
2020-04-09 06:48:00 2020-04-09 06:40:40.559719 6000.0 5300.0 1.0 800.0
2020-04-09 06:49:00 NaT NaN NaN NaN NaN
2020-04-09 06:50:00 NaT NaN NaN NaN NaN
2020-04-09 06:51:00 NaT NaN NaN NaN NaN
2020-04-09 06:52:00 2020-04-09 16:50:38.559871 3600.0 5360.0 2.0 60.0
2020-04-09 06:53:00 NaT NaN NaN NaN NaN
2020-04-09 06:54:00 2020-04-29 16:50:38.559871 4200.0 5500.0 2.0 200.0
现在我想根据不同的条件填写NaN / NaT值,并在缺少df时添加一些行:
预期输出:
Time Power Total Energy ID Energy
2020-04-09 06:41:00 2020-04-09 06:40:40.559719 0 4500.0 1.0 0
2020-04-09 06:42:00 2020-04-09 06:40:40.559719 7500.0 4625.0 1.0 125.0
2020-04-09 06:43:00 2020-04-09 06:40:40.559719 7500.0 4750.0 1.0 250.0
2020-04-09 06:44:00 2020-04-09 06:40:40.559719 7500.0 4875.0 1.0 375.0
2020-04-09 06:45:00 2020-04-09 06:40:40.559719 7500.0 5000.0 1.0 500.0
2020-04-09 06:46:00 2020-04-09 06:40:40.559719 6000.0 5100.0 1.0 600.0
2020-04-09 06:47:00 2020-04-09 06:40:40.559719 6000.0 5200.0 1.0 700.0
2020-04-09 06:48:00 2020-04-09 06:40:40.559719 6000.0 5300.0 1.0 800.0
2020-04-09 06:49:00 - 0 5300.0 - 0
2020-04-09 06:50:00 - 0 5300.0 - 0
2020-04-09 06:51:00 2020-04-09 16:50:38.559871 0 5300.0 2.0 0
2020-04-09 06:52:00 2020-04-09 16:50:38.559871 3600.0 5360.0 2.0 60.0
2020-04-09 06:53:00 2020-04-09 16:50:38.559871 4200.0 5430.0 2.0 130.0
2020-04-09 06:54:00 2020-04-29 16:50:38.559871 4200.0 5500.0 2.0 200.0
感谢您的帮助! :)