【问题标题】:SQL-style explode on Dask Series or DataFrame columnDask Series 或 DataFrame 列上的 SQL 样式爆炸
【发布时间】:2019-04-02 19:23:35
【问题描述】:

我有一个 Dask 系列,其中包含一个包含值列表的列。我想执行一个 SQL 风格的分解来为每个索引值和相应的列表元素创建一个新行。对于这个特殊的问题,列表的长度都是一样的。

单行示例:

索引          
123   [值1,值2,值3]

所需的转换:

索引          
123              值1
123              价值2
123              值3

任何关于如何实现这一点的建议将不胜感激。

【问题讨论】:

    标签: python dataframe explode series dask


    【解决方案1】:

    在 pandas 数据帧上,这可能看起来像

    df.column.apply(pd.Series, 1).stack().reset_index(level=1, drop=True)
    

    要对 Dask 数据框执行此操作,您需要使用 map_partitions 对数据的每个分区执行完全相同的操作:

    def func(df):
        return df.column.apply(pd.Series, 1).stack().reset_index(level=1, drop=True)
    df.map_partitions(func)
    

    【讨论】:

    • 对于系列,您不需要.column
    • 非常感谢!
    • 太棒了!如果你想给'exploded'列一个想要的名字,在reset_index(..)之后添加.to_frame('desired_exploded_col_name')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 2018-11-25
    • 2018-04-19
    • 1970-01-01
    • 2022-06-18
    • 2020-04-10
    相关资源
    最近更新 更多