【问题标题】:Adding column with values from one dataframe to another based on same datetime基于相同的日期时间将具有值的列从一个数据帧添加到另一个数据帧
【发布时间】:2021-03-29 03:55:33
【问题描述】:

我有一个包含日期时间和值的大数据框,所以我将采用一个名为 df1 的示例数据框

DateTime            Values
2020-12-26 14:00:00 439.0
2020-12-26 14:00:00 441.0
2020-12-26 14:00:00 461.0
2020-12-27 08:00:00 536.0
2020-12-27 08:00:00 547.0
2020-12-27 08:00:00 484.0
2020-12-27 08:00:00 497.0
2020-12-27 14:00:00 491.0
2020-12-27 14:00:00 512.0
2020-12-27 14:00:00 529.0
2020-12-27 14:00:00 436.0

我还有另一个完整的数据框 df 2

DateTime            Trend   
2020-12-18 14:00    no trend    
2020-12-19 08:00    no trend    
2020-12-19 14:00    no trend    
2020-12-21 08:00    no trend        
2020-12-21 14:00    no trend    
2020-12-22 08:00    no trend    
2020-12-22 14:00    decreasing  
2020-12-23 08:00    no trend    
2020-12-23 14:00    no trend    
2020-12-24 08:00    no trend    
2020-12-24 14:00    no trend    
2020-12-25 08:00    no trend    
2020-12-25 14:00    decreasing  
2020-12-26 08:00    no trend
2020-12-26 14:00    decreasing  
2020-12-27 08:00    no trend    
2020-12-27 14:00    no trend    

我正在尝试创建一个新列 df1[Trend] 并根据正确的日期时间分配趋势值

结果应该是这样的

DateTime            Values    Trend
2020-12-26 14:00:00 439.0     decreasing
2020-12-26 14:00:00 441.0     decreasing
2020-12-26 14:00:00 461.0     decreasing
2020-12-27 08:00:00 536.0     no trend
2020-12-27 08:00:00 547.0     no trend
2020-12-27 08:00:00 484.0     no trend
2020-12-27 08:00:00 497.0     no trend
2020-12-27 14:00:00 491.0     no trend
2020-12-27 14:00:00 512.0     no trend
2020-12-27 14:00:00 529.0     no trend
2020-12-27 14:00:00 436.0     no trend

我有办法吗?

【问题讨论】:

    标签: python python-3.x pandas dataframe datetime


    【解决方案1】:

    使用pandas.DataFrame.merge:

    df1.merge(df2)
    
                  DateTime  Values       Trend
    0  2020-12-26 14:00:00   439.0  decreasing
    1  2020-12-26 14:00:00   441.0  decreasing
    2  2020-12-26 14:00:00   461.0  decreasing
    3  2020-12-27 08:00:00   536.0    no trend
    4  2020-12-27 08:00:00   547.0    no trend
    5  2020-12-27 08:00:00   484.0    no trend
    6  2020-12-27 08:00:00   497.0    no trend
    7  2020-12-27 14:00:00   491.0    no trend
    8  2020-12-27 14:00:00   512.0    no trend
    9  2020-12-27 14:00:00   529.0    no trend
    10 2020-12-27 14:00:00   436.0    no trend
    

    编辑

    你得到了

    ValueError:您正在尝试合并 datetime64[ns] 和对象列。如果你想继续,你应该使用 pd.concat

    因为df1["DateTime"] 是一个日期时间列,而df2["DateTime"] 是一个str 列,所以您不能合并。要修复它,请使用 pandas.to_datetimedf2["DateTime"] 转换为日期时间:

    df2['DateTime'] = pd.to_datetime(df2.DateTime)
    

    【讨论】:

    • 执行此操作时出现此错误。 ValueError:您正在尝试合并 datetime64[ns] 和对象列。如果您希望继续,您应该使用 pd.concat 但是通过执行 pd.concat if 只会水平组合两个数据帧并导致剩余的具有 nan 值,这不是我正在寻找的\
    • 转换成日期时间列df2['DateTime'] = pd.to_datetime(df2.DateTime).
    • @DDM 立即检查:P
    • 非常感谢,是的,由于对象类型
    • @DDM 不错!快乐编码:)
    猜你喜欢
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多