2016-04-11 118 views
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 
+1

分組可以調用組返回的一個字典後組和特定組的'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

回答

2

這裏是一個可能的方式做到這一點:

import pandas as pd 
from StringIO import StringIO 

st = """ 
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 
""" 

data = pd.read_csv(StringIO(st), delimiter="|", error_bad_lines=False) 

#get a list of unique ids 
ids = pd.unique(data["id"].values.ravel()) 

grouped_data = data.groupby("id") 
for id in ids: 
    #get the dataframe for the current id 
    df = grouped_data.get_group(id) 
    #export current id's dataframe to a csv file with its name 
    df.to_csv(str(id)+".csv", sep="|", index=False) 
+0

謝謝!我應該指出,對於csv,分隔符應該是「,」而不是「|」 – LearningSlowly