本文介绍了了解MultiIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我在csv中有一个示例数据集:-
So I have a sample data set like this in csv:-
name team date score
John A 3/9/12 100
John B 3/9/12 99
Jane B 4/9/12 102
Peter A 9/9/12 103
Josie C 11/9/12 111
Rachel A 30/10/12 98
Kate B 31/10/12 103
David C 1/11/12 104
执行以下操作:-
from pandas.io.parsers import read_csv
df = read_csv("data/Workbook1.csv", index_col=["team", "name"])
df
date score
team name
A John 3/9/12 100
B John 3/9/12 99
Jane 4/9/12 102
A Peter 9/9/12 103
C Josie 11/9/12 111
A Rachel 30/10/12 98
B Kate 31/10/12 103
C David 1/11/12 104
如何进一步压缩第一个索引(团队"),以使我没有重复的值?成为:-
How do I compress the first index ("team") further so that I don't have duplicate values? To become:-
date score
team name
A John 3/9/12 100
Peter 9/9/12 103
Rachel 30/10/12 98
B John 3/9/12 99
Jane 4/9/12 102
Kate 31/10/12 103
C Josie 11/9/12 111
David 1/11/12 104
推荐答案
自己弄清楚了.
df = read_csv("data/Workbook1.csv")
df
name team date score
0 John A 3/9/12 100
1 John B 3/9/12 99
2 Jane B 4/9/12 102
3 Peter A 9/9/12 103
4 Josie C 11/9/12 111
5 Rachel A 30/10/12 98
6 Kate B 31/10/12 103
7 David C 1/11/12 104
df2 = df.pivot('team', 'name').stack()
df2
date score
team name
A John 3/9/12 100
Peter 9/9/12 103
Rachel 30/10/12 98
B Jane 4/9/12 102
John 3/9/12 99
Kate 31/10/12 103
C David 1/11/12 104
Josie 11/9/12 111
这篇关于了解MultiIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!