亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Pandas Dataframe - 無循環的組件和子組件編號系統

Pandas Dataframe - 無循環的組件和子組件編號系統

aluckdog 2024-01-27 15:28:04
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 )。要匹配上面的函數:BashOperatorcopy_data = PythonOperator(dag=dag,                           task_id='copy_data_task',                           provide_context=True,                           python_callable=copy_data_from_s3)
查看完整描述

1 回答

?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

鑰匙

應用于每行的輸出變化可以完全由當前“級別”和前一個級別確定。這里“級別”表示具有非零條目的列的索引號。


換句話說,保留前一行級別的狀態變量足以正確填充當前行。


代碼

# the working dataset

df2 = df.iloc[:, :4].reset_index(drop=True)? # make a copy

df2.columns = range(4)? # rename columns to (0,1,2,3) for convenience


# output container

arr = np.zeros(df2.shape, dtype=int)?


# state variable: level of the last row

last_lv = 0


for 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 dataframe

df[["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 IV

0? ? ? ? ?1? ? ? ? ?0? ? ? ? ? 0? ? ? ? ?0

1? ? ? ? ?1? ? ? ? ?1? ? ? ? ? 0? ? ? ? ?0

2? ? ? ? ?1? ? ? ? ?1? ? ? ? ? 1? ? ? ? ?0

3? ? ? ? ?1? ? ? ? ?1? ? ? ? ? 2? ? ? ? ?0

4? ? ? ? ?1? ? ? ? ?1? ? ? ? ? 3? ? ? ? ?0

5? ? ? ? ?1? ? ? ? ?1? ? ? ? ? 3? ? ? ? ?1

6? ? ? ? ?1? ? ? ? ?1? ? ? ? ? 3? ? ? ? ?2

7? ? ? ? ?1? ? ? ? ?1? ? ? ? ? 3? ? ? ? ?3

8? ? ? ? ?1? ? ? ? ?2? ? ? ? ? 0? ? ? ? ?0

9? ? ? ? ?1? ? ? ? ?2? ? ? ? ? 1? ? ? ? ?0

10? ? ? ? 1? ? ? ? ?2? ? ? ? ? 2? ? ? ? ?0

11? ? ? ? 1? ? ? ? ?2? ? ? ? ? 3? ? ? ? ?0

12? ? ? ? 1? ? ? ? ?2? ? ? ? ? 3? ? ? ? ?1

13? ? ? ? 1? ? ? ? ?2? ? ? ? ? 3? ? ? ? ?2

14? ? ? ? 1? ? ? ? ?2? ? ? ? ? 3? ? ? ? ?3

15? ? ? ? 2? ? ? ? ?0? ? ? ? ? 0? ? ? ? ?0

16? ? ? ? 2? ? ? ? ?1? ? ? ? ? 0? ? ? ? ?0

17? ? ? ? 2? ? ? ? ?1? ? ? ? ? 1? ? ? ? ?0

筆記

  1. 該代碼只是演示了處理每一行時的邏輯是什么樣的。它尚未完全優化,因此當效率成為問題時,請考慮使用更有效的數據表示形式(例如 numpy 數組或只是級別數字列表)。

  2. 我調查了任何tree數據結構的庫,例如anytree和treelib,希望找到一種自動輸出樹層次結構的自動化方法。不幸的是,似乎缺乏適合讀取縮進文本文件或類似格式的 I/O 函數。這是我決定重新發明輪子的主要原因。


查看完整回答
反對 回復 2024-01-27
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號