【问题标题】:Merge values of a dataframe where other columns match合并其他列匹配的数据框的值
【发布时间】:2021-04-17 18:38:23
【问题描述】:

我有一个存储日期、汽车品牌、颜色和城市的数据框:

 date              car_brand    color     city
 "2020-01-01"      porsche      red       paris
 "2020-01-02"      prosche      red       paris
 "2020-01-03"      porsche      red       london
 "2020-01-04"      porsche      red       paris
 "2020-01-05"      porsche      red       london
 "2020-01-01"      audi         blue      munich
 "2020-01-02"      audi         red       munich
 "2020-01-03"      audi         red       london
 "2020-01-04"      audi         red       london
 "2020-01-05"      audi         red       london

我现在想通过以下方式从该数据框创建: 将汽车品牌、颜色和城市连续几天匹配的行合并在一起。所以在这个例子中,我想以一个数据框结束

 date                             car_brand    color     city
 ["2020-01-01","2020-01-02"]      porsche      red       paris
 ["2020-01-03"]                   porsche      red       london
 ["2020-01-04"]                   porsche      red       paris
 ["2020-01-05"]                   porsche      red       london
 ["2020-01-01"]                   audi         blue      munich
 ["2020-01-02"]                   audi         red       munich
 ["2020-01-03","2020-01-05"]      audi         red       london

我怎样才能做到这一点?我尝试使用 pd.concat 和 pd.merge 但到目前为止没有任何效果。谢谢!

【问题讨论】:

    标签: python pandas dataframe merge


    【解决方案1】:

    如果连续很重要,可以检查列表理解。这是从组上的lambda 函数获取list 的技术扩展。

    df = pd.read_csv(io.StringIO(""" date              car_brand    color     city
     "2020-01-01"      porsche      red       paris
     "2020-01-02"      porsche      red       paris
     "2020-01-03"      porsche      red       london
     "2020-01-04"      porsche      red       paris
     "2020-01-05"      porsche      red       london
     "2020-01-01"      audi         blue      munich
     "2020-01-02"      audi         red       munich
     "2020-01-03"      audi         red       london
     "2020-01-04"      audi         red       london
     "2020-01-05"      audi         red       london"""), sep="\s+")
    df["date"] = pd.to_datetime(df["date"])
    df = (
        df
        .groupby([c for c in df.columns if c!="date"])["date"]
        # only include if first date or if it's a consequetive date
        .agg(lambda x: [xx for i,xx in enumerate(x) if i==0 or xx==(list(x)[i-1]+pd.DateOffset(1))])
        .reset_index()
    )
    
    

    输出

    car_brand color   city                                                            date
         audi  blue munich                                           [2020-01-01 00:00:00]
         audi   red london [2020-01-03 00:00:00, 2020-01-04 00:00:00, 2020-01-05 00:00:00]
         audi   red munich                                           [2020-01-02 00:00:00]
      porsche   red london                                           [2020-01-03 00:00:00]
      porsche   red  paris                      [2020-01-01 00:00:00, 2020-01-02 00:00:00]
    

    【讨论】:

    • 谢谢。但不幸的是,我收到错误 ValueError: Function does not reduce
    • 带有样本数据还是您的实际数据集?你用的是什么版本的 python 和 pandas?
    • 带有示例数据,Python 3.6.9,Pandas 版本 '0.22.0'
    • 更新熊猫版本成功了谢谢!
    • 那是相当老的熊猫版本了。我正在使用 1.2.0,0.22.0 无法在我的虚拟环境中安装。你可以试试住宅区日期版的熊猫吗?我将构建一个与您匹配的环境,但需要一些时间
    猜你喜欢
    • 2022-11-07
    • 1970-01-01
    • 2021-02-10
    • 2021-07-22
    • 1970-01-01
    • 2015-11-21
    • 2022-12-05
    • 2016-12-27
    • 1970-01-01
    相关资源
    最近更新 更多