0
我有一個數據幀,df
它看起來像這樣:迭代創建的CSV文件,通過一系列的熊貓
Site Roadname Count id Count_norm
9 A316 Twickenham Rd, Richmond 1474 9SOUTHBOUND 1428
9 A316 Twickenham Rd, Richmond 1375 9SOUTHBOUND 1329
9 A316 Twickenham Rd, Richmond 1052 9SOUTHBOUND 1006
9 A316 Twickenham Rd, Richmond 986 9SOUTHBOUND 940
9 A316 Twickenham Rd, Richmond 1071 9SOUTHBOUND 1025
9 A316 Twickenham Rd, Richmond 1206 9SOUTHBOUND 1160
9 A316 Twickenham Rd, Richmond 1474 9NORTHBOUND 1428
9 A316 Twickenham Rd, Richmond 1375 9NORTHBOUND 1329
9 A316 Twickenham Rd, Richmond 1052 9NORTHBOUND 1006
9 A316 Twickenham Rd, Richmond 986 9NORTHBOUND 940
9 A316 Twickenham Rd, Richmond 1071 9NORTHBOUND 1025
9 A316 Twickenham Rd, Richmond 1206 9NORTHBOUND 1160
我可以通過一系列創建單獨的CSV的:
11N_series = results[results.id == "11NORTHBOUND"]
11N_series.to_csv('./11NORTHBOUND.csv')
然而,這需要我定義每個系列的名稱(id
)
如何迭代通過df
數據框並按導出csv?
我可以看到每個ID的數量和名稱:
[in] id_count = results.groupby(["id"]).size()
print(id_count)
[out]
id
11NORTHBOUND 467
11SOUTHBOUND 467
15NORTHBOUND 467
分組可以調用組返回的一個字典後組和特定組的'get_group'將返回一個在該組上鍵入的df,請參閱:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.groups.html# pandas.core.groupby.GroupBy.groups和http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.get_group.html#pandas.core.groupby.GroupBy.get _group – EdChum