【问题标题】:Python/Pandas merge issue with NaN dataPython/Pandas 与 NaN 数据的合并问题
【发布时间】:2017-03-13 15:48:39
【问题描述】:

我正在尝试使用 pd.concat 将两个数据帧(dfdf2)合并为一个新的数据帧(df3) Pandas 使用以下代码:

df3 = pd.concat([df, df2])

这几乎按照我想要的方式工作,但它会产生一个问题。

df 包含当前日期的数据,索引是时间序列。它看起来像这样:

                        Facility    Servers   PUE
2016-10-31  00:00:00    6.0         5.0       1.2
2016-10-31  00:30:00    7.0         5.0       1.4
2016-10-31  01:00:00    6.0         5.0       1.2
2016-10-31  01:30:00    6.0         5.0       1.2
2016-10-31  02:00:00    6.0         5.0       1.2

df2 仅包含 NaN 数据,索引是一个时间序列,其格式与 df 中的序列相对应,但从较早的日期开始并持续完整年(即 17520 行对应于 365 * 48 三十分钟间隔)。它看起来基本上是这样的:

                        Facility    Servers   PUE
2016-10-01  00:00:00    NaN         NaN       NaN
2016-10-01  00:30:00    NaN         NaN       NaN
2016-10-01  01:00:00    NaN         NaN       NaN
2016-10-01  01:30:00    NaN         NaN       NaN
2016-10-01  02:00:00    NaN         NaN       NaN
2016-10-01  02:30:00    NaN         NaN       NaN
<continues to 17520 rows, i.e. one year of 30 minute time intervals>

当我申请时:df3 = pd.concat([df, df2])

然后运行df3.head(),我得到以下信息:

                        Facility    Servers   PUE
2016-10-31  00:00:00    6.0         5.0       1.2
2016-10-31  00:30:00    7.0         5.0       1.4
2016-10-31  01:00:00    6.0         5.0       1.2
2016-10-31  01:30:00    6.0         5.0       1.2
2016-10-31  02:00:00    6.0         5.0       1.2
2016-10-31  02:30:00    NaN         NaN       NaN
2016-10-31  03:00:00    NaN         NaN       NaN
2016-10-31  03:30:00    NaN         NaN       NaN
<continues to the end of the year>

换句话说,代码似乎删除了 df 中数据之前发生的时间间隔内的所有 NaN 数据。谁能建议如何保留 df2 中的所有数据,仅将其替换为 df 中相应时间间隔的数据?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    使用combine_first

    result = df1.combine_first(df2)
    

    如果左侧 DataFrame 中缺少值,result 只会从右侧 DataFrame 中获取值

    【讨论】:

    • 谢谢你。
    【解决方案2】:

    我认为你需要reindexunion indexes

    print (df2.index.union(df.index))
    DatetimeIndex(['2016-10-01 00:00:00', '2016-10-01 00:30:00',
                   '2016-10-01 01:00:00', '2016-10-01 01:30:00',
                   '2016-10-01 02:00:00', '2016-10-01 02:30:00',
                   '2016-10-31 00:00:00', '2016-10-31 00:30:00',
                   '2016-10-31 01:00:00', '2016-10-31 01:30:00',
                   '2016-10-31 02:00:00'],
                  dtype='datetime64[ns]', freq=None)
    
    df = df.reindex(df2.index.union(df.index))
    print (df)
                         Facility  Servers  PUE
    2016-10-01 00:00:00       NaN      NaN  NaN
    2016-10-01 00:30:00       NaN      NaN  NaN
    2016-10-01 01:00:00       NaN      NaN  NaN
    2016-10-01 01:30:00       NaN      NaN  NaN
    2016-10-01 02:00:00       NaN      NaN  NaN
    2016-10-01 02:30:00       NaN      NaN  NaN
    2016-10-31 00:00:00       6.0      5.0  1.2
    2016-10-31 00:30:00       7.0      5.0  1.4
    2016-10-31 01:00:00       6.0      5.0  1.2
    2016-10-31 01:30:00       6.0      5.0  1.2
    2016-10-31 02:00:00       6.0      5.0  1.2
    

    【讨论】:

    • 谢谢。正是我需要的。
    猜你喜欢
    • 1970-01-01
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2020-12-27
    • 2020-03-12
    • 2022-08-16
    相关资源
    最近更新 更多