[英]Pandas Groupby - No objects to concatenate
我有一個具有以下值的數據框:
df =
route stop_code stop_name
900 92072 Eastbound @ 257 Kingston Road East
900 1590 Kingston Westbound @ Wicks
900 2218 Kingston Eastbound @ Wicks
900 93152 Salem Northbound @ Kingston
92 728 Kingston Rd. @ Salem Rd.
224 92071 Salem Southbound @ Twilley
215 92071 Salem Southbound @ Twilley
215 92054 Northbound @ 133 Salem
224 92054 Northbound @ 133 Salem
215 93152 Salem Northbound @ Kingston
我想要的是按 stop_code 或 stop_name 對路線進行分組,例如:
df2 =
route stop_code stop_name
900 92072 Eastbound @ 257 Kingston Road East
900 1590 Kingston Westbound @ Wicks
900 2218 Kingston Eastbound @ Wicks
92 728 Kingston Rd. @ Salem Rd.
224, 215 92071 Salem Southbound @ Twilley
215, 215 92054 Northbound @ 133 Salem
215, 900 93152 Salem Northbound @ Kingston
我嘗試執行以下操作:
df2 = df.groupby(['stop_code']).agg(set).reset_index()
雖然它在我的測試環境中運行良好,但當我在 Django (Python Anywhere) 中部署它時,出現以下錯誤(可能是由於 Pandas / Python / Django 的不同版本):
ValueError: No objects to concatenate
任何人都可以請指導我整理一下嗎? TIA
你可以這樣做
df.groupby(['stop_code','stop_name'],sort=False)['route'].agg(list).reset_index()
輸出
stop_code stop_name route
0 92072 Eastbound @ 257 Kingston Road East [900]
1 1590 Kingston Westbound @ Wicks [900]
2 2218 Kingston Eastbound @ Wicks [900]
3 93152 Salem Northbound @ Kingston [900, 215]
4 728 Kingston Rd. @ Salem Rd. [92]
5 92071 Salem Southbound @ Twilley [224, 215]
6 92054 Northbound @ 133 Salem [215, 224]
如果您想要完全按照您的預期輸出,請執行以下操作。 但是,這可能會更慢。
df.groupby(['stop_code','stop_name'],sort=False)['route'].apply(lambda x: ','.join([str(a) for a in x])).reset_index()
輸出
stop_code stop_name route
0 92072 Eastbound @ 257 Kingston Road East 900
1 1590 Kingston Westbound @ Wicks 900
2 2218 Kingston Eastbound @ Wicks 900
3 93152 Salem Northbound @ Kingston 900,215
4 728 Kingston Rd. @ Salem Rd. 92
5 92071 Salem Southbound @ Twilley 224,215
6 92054 Northbound @ 133 Salem 215,224
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.