我有两个引用气象站的数据框:
import pandas as pd
df_shift = pd.DataFrame({'Date': ['2010-10-05', '2010-10-20', '2011-03-15',
'2012-03-22', '2015-01-17', '2015-01-23',
'2015-01-30'],
'Sensor_id': [1024, 1024, 1024, 1024,
2210, 2210, 1010]})
df_station = pd.DataFrame({'Sensor_id': [1024, 1024, 1024, 2210, 2210],
'Sensor_type': ['analog', 'analog', 'analog', 'dig', 'dig'],
'Date': ['2010-10-01', '2010-10-22', '2011-03-14',
'2015-01-13', '2015-01-22']})
我想在 df_station 中创建一个新列,这个列称为“new_column”。
我希望此列填充数据框(班次和气象站)的日期字段之间的天数差异较少。
我做了以下代码:
# Starting with a very large value
df_station['new_column'] = 90000
for i in range(0, len(df_station)):
for j in range(0, len(df_shift)):
var_Difference_Date = abs(pd.to_datetime(df_station['Date'].iloc[i],
format='%Y/%m/%d') -
pd.to_datetime(df_shift['Date'].iloc[j], format='%Y/%m/%d'))
if(df_station['Sensor_id'].iloc[i] == df_shift['Sensor_id'].iloc[j]):
if(var_Difference_Date.days < df_station['new_column'].iloc[i]):
df_station['new_column'].loc[i] = var_Difference_Date.days
显示结果,符合预期:
Sensor_id Sensor_type Date new_column
1024 analog 2010-10-01 4
1024 analog 2010-10-22 2
1024 analog 2011-03-14 1
2210 dig 2015-01-13 4
2210 dig 2015-01-22 1
但是,有没有更有效的方法来做到这一点而不必使用两个 For()?谢谢。
阿晨1998
DIEA
小唯快跑啊
回首忆惘然
相关分类