【问题标题】:In DASK, how does one add a range of integers(auto-increment) to a new column?在 DASK 中,如何将一系列整数(自动增量)添加到新列?
【发布时间】:2020-03-24 12:56:58
【问题描述】:

我需要向我的 DASK 数据框添加一列,该列应包含自动增量 ID。我知道如何在 Pandas 中执行此操作,因为我在 SO 上找到了 Pandas 解决方案,但我无法弄清楚如何在 DASK 中执行此操作。我最好的尝试是这样的,结果发现自动增量功能只为我的 100 行测试文件运行了两次,并且所有的 id 都是 2。

def autoincrement(self):
    print('*')
    self.report_line = self.report_line + 1
    return self.report_line

self.df = self.df.map_partitions(
    lambda df: df.assign(raw_report_line=self.autoincrement())
)

Pandas 的方式看起来像这样

df.insert(0, 'New_ID', range(1, 1 + len(df)))

或者,如果我可以获取特定 CSV 行的行号并将其添加到列中,那就太好了,在现阶段,这似乎不太可能。

【问题讨论】:

    标签: python pandas dask dask-dataframe


    【解决方案1】:

    您可以分配一个全为 1 的虚拟列并取 cumsum

    In [1]: import dask.datasets
    
    In [2]: import pandas as pd
    
    In [3]: import numpy as np
    
    In [4]: df = dask.datasets.timeseries()
    
    In [5]: df
    Out[5]:
    Dask DataFrame Structure:
                       id    name        x        y
    npartitions=30
    2000-01-01      int64  object  float64  float64
    2000-01-02        ...     ...      ...      ...
    ...               ...     ...      ...      ...
    2000-01-30        ...     ...      ...      ...
    2000-01-31        ...     ...      ...      ...
    Dask Name: make-timeseries, 30 tasks
    
    In [6]: df['row_number'] = df.assign(partition_count=1).partition_count.cumsum()
    
    In [7]: df.compute()
    Out[7]:
                           id      name         x         y  row_number
    timestamp
    2000-01-01 00:00:00   928     Sarah -0.597784  0.160908           1
    2000-01-01 00:00:01  1000     Zelda -0.034756 -0.073912           2
    2000-01-01 00:00:02  1028  Patricia -0.962331 -0.458834           3
    2000-01-01 00:00:03  1010    Hannah -0.225759 -0.227945           4
    2000-01-01 00:00:04   958   Charlie  0.223131 -0.672307           5
    ...                   ...       ...       ...       ...         ...
    2000-01-30 23:59:55  1052     Jerry -0.636159  0.683076     2591996
    2000-01-30 23:59:56   973     Quinn -0.575324  0.272144     2591997
    2000-01-30 23:59:57  1049     Jerry  0.143286 -0.122490     2591998
    2000-01-30 23:59:58   971    Victor -0.866174  0.751534     2591999
    2000-01-30 23:59:59   966     Edith -0.718382 -0.333261     2592000
    
    [2592000 rows x 5 columns]
    

    【讨论】:

      猜你喜欢
      • 2021-03-21
      • 2019-01-18
      • 1970-01-01
      • 2022-01-20
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      相关资源
      最近更新 更多