Python,Pandas,如何基于列分隔数据帧

时间:2017-03-15 14:44:50

标签: python pandas

说我的数据框看起来像这样:

mydata = [{'gender' : 'F', 'session': 0, 'score':23},
          {'gender' : 'F', 'session': 1, 'score':24},
       {'gender' : 'M', 'session': 0, 'score':22},
       {'gender' : 'M', 'session': 1, 'score':32},
       {'gender' : 'F', 'session': 0, 'score':33},
          {'gender' : 'F', 'session': 1, 'score':33},
       {'gender' : 'M', 'session': 0, 'score':44},
       {'gender' : 'M', 'session': 1, 'score':44},
       ]
df = pd.DataFrame(mydata)


  gender  score  session
0      F     23        0
1      F     24        1
2      M     22        0
3      M     32        1
4      F     33        0
5      F     33        1
6      M     44        0
7      M     44        1

如何创建一些基于会话和性别的新分数变量: 比较female_0,female_1,male_0,male_1。

非常感谢

1 个答案:

答案 0 :(得分:1)

您需要dict comprehension groupby的IIUC:

dfs = {'{}_{}'.format(i[0], i[1]):df for i, df in df.groupby(['gender','session'])}
print (dfs)
{'F_1':   gender  score  session
1      F     24        1
5      F     33        1, 'M_0':   gender  score  session
2      M     22        0
6      M     44        0, 'M_1':   gender  score  session
3      M     32        1
7      M     44        1, 'F_0':   gender  score  session
0      F     23        0
4      F     33        0}

print (dfs['F_1'])
  gender  score  session
1      F     24        1
5      F     33        1