I have a dataframe called Traffic:
JavaScript
x
6
1
Source Method Destination Weight
2
0 LA Ground NY 20
3
1 LA Ground NY 15
4
2 LA Ground LV 10
5
3 LA Air LV 5
6
I’d like to end up with a dataframe like so:
JavaScript
1
3
1
Source Ground Air
2
0 LA LV: 10, NY: 35 LV: 5
3
Where the 4 rows are combined into 1 based off the Source. The traffic methods are then further broken up by their destinations in ascending order. If there are multiple entries from say LA->NY of type Ground, add the weights.
Ground/Air columns would be strings following the format of “Destination:TotalWeight”.
This pattern would continue all the way down for any other Sources such as ‘WA’…
Advertisement
Answer
initialize
JavaScript
1
6
1
df = pd.DataFrame(data= [["LA","Ground","NY",20],
2
["LA","Ground","NY",15],
3
["LA","Ground","LV",10],
4
["LA","Air","LV",5]],
5
columns=["Source","Method","Destination","Weight"])
6
do stuff
JavaScript
1
13
13
1
# add weights when same source, method & destination
2
df_ = df.groupby(["Source","Method","Destination"], as_index=False)['Weight'].sum()
3
# conatenate destination and weight strings
4
df_['Dest_Wt'] = df_['Destination'].str.cat(df_['Weight'].astype(str), sep = ': ')
5
6
shipping_summary = (df_.groupby(["Source","Method"])['Dest_Wt']
7
.apply(', '.join)
8
.unstack(level=1)
9
.reset_index())
10
shipping_summary.columns.name = None
11
12
print(shipping_summary)
13
output
JavaScript
1
3
1
Source Air Ground
2
0 LA LV: 5 LV: 10, NY: 35
3