I want to transform/pivot the following dataframe to indicate the immediate data flow from (source) to (target).:
l0 l1 l2 l3 sum
0 IN TOTAL <NA> <NA> 1
1 <NA> TOTAL OUT_A OUT_B 2
2 <NA> TOTAL <NA> OUT_C 3
In the above example, a data flow is represented by e.g. l0 to l1 in row 0. Equivalently, l1 to l2 and l2 to l3 represent (direct) data flows in row 1, as well as l1 to l3 in row 2.
Expectation:
source target sum
0 IN TOTAL 1
1 TOTAL OUT_A 2
2 TOTAL OUT_C 3
3 OUT_A OUT_B 2
For reproducibility:
import pandas as pd
df = pd.DataFrame({
"l0": ['IN', pd.NA, pd.NA],
"l1": ['TOTAL','TOTAL','TOTAL'],
"l2": [pd.NA,'OUT_A', pd.NA],
"l3": [pd.NA,'OUT_B',"OUT_C"],
"sum": [1,2,3]})
pd.DataFrame({
"source": ["IN","TOTAL","TOTAL","OUT_A"],
"target": ["TOTAL","OUT_A","OUT_C","OUT_B"],
"sum": [1,2,3,2]
})