Pandas Dataframe - 无循环的组件和子组件编号系统

import papermill as pm

# ...

# define DAG, etc.

# ...


def copy_data_from_s3(**context):

    pm.execute_notebook(

           "copy_data_from_s3_step.ipynb",

           "copy_data_from_s3_step.ipynb"

            parameters=dict(date=context['execution_date'])) # pass some context parameter if you need to

        )

最后,设置该步骤,也许作为 a (尽管如果您想从命令行运行 PapermillPythonOperator也可以使用 a )。要匹配上面的函数:BashOperator

copy_data = PythonOperator(dag=dag,

                           task_id='copy_data_task',

                           provide_context=True,

                           python_callable=copy_data_from_s3)


aluckdog
浏览 32回答 1
1回答

慕工程0101907

钥匙应用于每行的输出变化可以完全由当前“级别”和前一个级别确定。这里“级别”表示具有非零条目的列的索引号。换句话说,保留前一行级别的状态变量足以正确填充当前行。代码# the working datasetdf2 = df.iloc[:, :4].reset_index(drop=True)  # make a copydf2.columns = range(4)  # rename columns to (0,1,2,3) for convenience# output containerarr = np.zeros(df2.shape, dtype=int) # state variable: level of the last rowlast_lv = 0for idx, row in df2.iterrows():    # get current indentation level    lv = row.first_valid_index()    if idx > 0:        # case 1: same or decreased level        if lv <= last_lv:            # keep previous levels except current level            arr[idx, :lv] = arr[idx-1, :lv]            # current level++            arr[idx, lv] = arr[idx-1, lv] + 1        # case 2: increased level        elif lv > last_lv:            # keep previous levels            arr[idx, :last_lv+1] = arr[idx - 1, :last_lv+1]            # start counting the new levels            arr[idx, last_lv+1:lv+1] = 1      # the first row    else:        arr[0, 0] = 1    # update state variable for next use    last_lv = lv# append result to dataframedf[["Level I", "Level II", "Level III", "Level IV"]] = arr结果print(df[["Level I", "Level II", "Level III", "Level IV"]])    Level I  Level II  Level III  Level IV0         1         0          0         01         1         1          0         02         1         1          1         03         1         1          2         04         1         1          3         05         1         1          3         16         1         1          3         27         1         1          3         38         1         2          0         09         1         2          1         010        1         2          2         011        1         2          3         012        1         2          3         113        1         2          3         214        1         2          3         315        2         0          0         016        2         1          0         017        2         1          1         0笔记该代码只是演示了处理每一行时的逻辑是什么样的。它尚未完全优化,因此当效率成为问题时,请考虑使用更有效的数据表示形式(例如 numpy 数组或只是级别数字列表)。我调查了任何tree数据结构的库,例如anytree和treelib,希望找到一种自动输出树层次结构的自动化方法。不幸的是,似乎缺乏适合读取缩进文本文件或类似格式的 I/O 函数。这是我决定重新发明轮子的主要原因。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python