Pandas - 连接具有相同值的行,写入csv

时间:2017-04-20 13:50:35

标签: python pandas

我在Pandas中创建了一个如下所示的数据框:

 disadvantaged_team seconds_left num_ic_inc
0                ATL            5          2
  disadvantaged_team seconds_left num_ic_inc
1                ATL           10          2
  disadvantaged_team seconds_left num_ic_inc
2                ATL           15          4
  disadvantaged_team seconds_left num_ic_inc
3                ATL           20          1
  disadvantaged_team seconds_left num_ic_inc
4                ATL           25          1
  disadvantaged_team seconds_left num_ic_inc
5                ATL           30          3
  disadvantaged_team seconds_left num_ic_inc
6                ATL           40          2
  disadvantaged_team seconds_left num_ic_inc
7                ATL           45          3
  disadvantaged_team seconds_left num_ic_inc
8                ATL           50          1
  disadvantaged_team seconds_left num_ic_inc
9                ATL           55          3
   disadvantaged_team seconds_left num_ic_inc
10                ATL           60          3
   disadvantaged_team seconds_left num_ic_inc
11                ATL           65          1
   disadvantaged_team seconds_left num_ic_inc
12                ATL           70          1
   disadvantaged_team seconds_left num_ic_inc
13                ATL           75          2
   disadvantaged_team seconds_left num_ic_inc
14                ATL           85          3
   disadvantaged_team seconds_left num_ic_inc
15                ATL           90          1
   disadvantaged_team seconds_left num_ic_inc
16                ATL           95          2
   disadvantaged_team seconds_left num_ic_inc
17                ATL          110          2
   disadvantaged_team seconds_left num_ic_inc
18                ATL          115          2
   disadvantaged_team seconds_left num_ic_inc
19                ATL          120          1
   disadvantaged_team seconds_left num_ic_inc
20                ATL          197          1
   disadvantaged_team seconds_left num_ic_inc
21                ATL          278          1
   disadvantaged_team seconds_left num_ic_inc
22                BKN            5          2
   disadvantaged_team seconds_left num_ic_inc
23                BKN           10          1
   disadvantaged_team seconds_left num_ic_inc
24                BKN           15          4
   disadvantaged_team seconds_left num_ic_inc
25                BKN           20          2
   disadvantaged_team seconds_left num_ic_inc
26                BKN           25          1
   disadvantaged_team seconds_left num_ic_inc
27                BKN           30          1
   disadvantaged_team seconds_left num_ic_inc
28                BKN           40          1
   disadvantaged_team seconds_left num_ic_inc
29                BKN           45          1
   disadvantaged_team seconds_left num_ic_inc
30                BKN           50          2
   disadvantaged_team seconds_left num_ic_inc
31                BKN           55          1
   disadvantaged_team seconds_left num_ic_inc
32                BKN           60          1
   disadvantaged_team seconds_left num_ic_inc
33                BKN           70          1
   disadvantaged_team seconds_left num_ic_inc
34                BKN           75          1
   disadvantaged_team seconds_left num_ic_inc
35                BKN           80          1
   disadvantaged_team seconds_left num_ic_inc
36                BKN           85          1
   disadvantaged_team seconds_left num_ic_inc
37                BKN           90          1
   disadvantaged_team seconds_left num_ic_inc
38                BKN           95          1
   disadvantaged_team seconds_left num_ic_inc

现在我想将结果写入csv文件,具体取决于团队,例如包含ATL的所有行应该在一个文件中,而在另一个文件中包含BKN。我已经使用iterrows()将每一行写入一个单独的文件,但正如我所说,我希望团队中的所有行都在一个csv中。

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

for value in df['disadvantaged_team'].unique(): 
    df[df['disadvantaged_team'] == value].to_csv(value + '.csv')

未选中,但逻辑成立:

1)获取系列中所有唯一值的列表

2)使用值来过滤主df

3)将结果写入CSV

答案 1 :(得分:0)

一个快速解决方案可以分组:

for g,f in df.groupby("disadvantaged_team"): f.to_csv(g+'.csv')

或以其他方式:

df.groupby("disadvantaged_team").apply(lambda f : f.to_csv(x["disadvantaged_team"].max()+'.csv'))