I have a dataset which has two destinations and the distance between them.
| city1 | city2 | distance |
|---|---|---|
| 101 | 102 | 56 |
| 102 | 103 | 34 |
| 103 | 104 | 6 |
| 104 | 105 | 15 |
I want to prepare a table which will have the cumulative distance with city1 as the index and city2 as the columns! and the values of the distance must be cumulative sum.The diagonal elements must be 0 as well!
Tried this using the cumsum() to first find the cumulative sum, and added it as a new column -
df3['cs'] = np.cumsum(df3['distance'])
I tried doing this using the pivot method.
pivot_table = df3.pivot(index='city1', columns='city2', values='cs')But the resulting table had the following output:

I want result having diagonal columns 0 and the others being the cumulative sum....
Anyone with a solution to it?
Expected Output -
