I have several lines defined in a dataframe.
import pandas as pd
df = pd.DataFrame(
{
'from': ['p2', 'p3', 'p1'],
'to': ['p3', 'p4', 'p2'],
},
index=['line_b', 'line_c', 'line_a'],
)
# How to get line_sequence as ['line_a', 'line_b', 'line_c']?
Each line has a from
point and a to
point. These lines are connected in certain sequence. In this example, the sequence is line_a --> line_b --> line_c
.
Could you please show me how to quickly find the connection sequence based on the columns of from
and to
? In the example above, there are numbers in the points' names, like 'p1'
and 'p2'
. It is just an example. In my real application, the names could be any string.
The expected outcome should be in the format of List[str]
.
Thanks.