简体   繁体   English

通过R / Python中两个不匹配的不规则时间序列来排列两个变量?

[英]Arrange two variables by two mismatching irregular time-series in R/Python?

I have date/time hourly that gives stream discharge and date/time on the hour at irregular intervals that gives stream sediment concentration. 我有每小时的日期/时间,用于给出水流的排放,而每小时的日期/时间以不规则的时间间隔,用于给出河流的沉积物浓度。 I'm unsure how to post data frames here, but it looks like: 我不确定如何在此处发布数据帧,但看起来像:

 Datetimedis, Discharge, Datetimesed, Sediment
6/12/15 12:00  1.1 6/12/15 18:00  1231
6/12/15 13:00  113 6/13/15 1:00 12312
6/12/15 14:00  123 21 6/13/15 8:00 12321
6/12/15 15:00  12 6/13/15 15:00 12312
6/12/15 16:00  12 6/14/15 19:00 4324
6/12/15 17:00  23 6/15/15 2:00 534523
6/12/15 18:00  123 6/15/15 9:00 52341

etc 等等

I have ~2500 raws of data for the discharge, and ~500 columns for sediment. 我有约2500原始数据用于排放,有约500列用于沉积物。 Is there any way to use ddply or an R package or python to paste the values of sediment next to the discharge value that corresponds to the same time? 有什么方法可以使用ddply或R包或python将沉积物的值粘贴到对应于同一时间的排放值旁边吗?

In this example data, I would want for instance the sediment value at 6/12/15 18:00 to paste next to the discharge value at that time. 在此示例数据中,我想例如将15年6月12日18:00处的沉积物值粘贴到当时的排放值旁边。

I need to paste them there with the space in-between containing NA values or empty values so that I can later interpolate them. 我需要在其中粘贴它们,并在它们之间包含NA值或空值的空间,以便以后可以对其进行插值。

Assuming you have one dataframe with the datetime and discharge(df) and another with the time and sediment concentration(df2) you could do the following with python pandas ... 假设您有一个带有日期时间和流量(df)的数据框,另一个带有时间和沉积物浓度(df2)的数据框,则可以对python熊猫执行以下操作...

create a new column with the time from Datetime in df: 使用df中Datetime的时间创建一个新列:

 df['hours'] = df.index.hour

then use the map function from pandas to map the sediment concentration in df2 to df: 然后使用熊猫的map函数将df2中的沉积物浓度映射到df:

df['Sediment']=df['hours'].map(df2)

I figured it out using the zoo package! 我用Zoo包弄清楚了!

For anyone in the future who uses this as reference, I split up the two separate date and value entries into separate data frames, d1 and d2. 对于将来任何以此为参考的人,我将两个单独的日期和值条目拆分为单独的数据框d1和d2。

zoo1 <- read.zoo(d1, header=TRUE)
zoo2 <- read.zoo(d2, header=TRUE)
zoomerge <- merge(d1, d2)

worked perfectly! 工作完美!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM