Pandas:如何在一列上合并多个具有相同列名的数据帧,并使用重复的值?

2024-09-26 22:07:44 发布

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

this question之后

I have two data sets acquired simultaneously by different acquisition systems with different sampling rates. One is very regular, and the other is not. I would like to create a single dataframe containing both data sets, using the regularly spaced timestamps (in seconds) as the reference for both. The irregularly sampled data should be interpolated on the regularly spaced timestamps.

我有完全相同的情况,但我的t列可能有重复项。 对于t重复的每一行,我想保留一个数据列最大的行

遵循原始示例:

df1:
    t   y1
0  0.0  0.0
1  0.5  0.5
2  1.0  1.0
3  1.0  3.0
4  1.5  1.5
5  2.0  2.0

df2:
    t    y2
0  0.00  0.00
1  0.34  1.02
2  1.01  3.03
3  1.40  4.20
4  1.60  4.80
5  1.70  5.10
6  2.01  6.03


df_combined:
     t   y1   y2
0  0.0  0.0  0.0
1  0.5  0.5  1.5
2  1.0  3.0  3.0
3  1.5  1.5  4.5
4  2.0  2.0  6.0

注意现在t=1.0,y1=3.0

我该怎么做


Tags: thedataishavesetsthisquestiontwo
2条回答

有三项任务:

  1. df1上删除重复项
  2. 插值df2
  3. 将两者合并

所以这里有一个解决方案

(df2.set_index('t')
     .reindex(new_idx)
     .interpolate('index')
     .reset_index()
     .merge(df1.sort_values('y1', ascending=False)
               .drop_duplicates('t'),    
            on='t', how='right')
)

输出:

     t   y2   y1
0  0.0  0.0  0.0
1  0.5  1.5  0.5
2  1.0  3.0  3.0
3  1.5  4.5  1.5
4  2.0  6.0  2.0

如果你处理的是“时间戳”,那么你就必须使用datetime包,这是一个重要的个人没有重点的一个和一个重要的时间序列预测以及

相关问题 更多 >

    热门问题