在 pandas 中操作堆叠数据框
Manipulate a stacked dataframe in pandas
我想在 pandas 中将堆叠数据帧转换为常规数据帧。
原始df为:
Score
Amy
Brian
Carl
test1
5
6
7
test2
3
2
4
叠加后:
0
test1
Amy
5
Brian
6
Carl
7
test2
Amy
3
Brian
2
Carl
4
我想要的结果如下:
name
score
test1
Amy
5
test1
Brian
6
test1
Carl
7
test2
Amy
3
test2
Brian
2
test2
Carl
4
如有任何建议,我们将不胜感激!
这比上面@Quang Hoang 的回答稍长,但也应该如此。它在堆叠 DataFrame
后拾取
test1 Amy 5
test1 Brian 6
test1 Carl 7
test2 Amy 3
test2 Brian 2
test2 Carl 4
# Reset index to flatten DataFrame
result = df_stk.reset_index().copy()
level_0 level_1 0
0 test1 Amy 5
1 test1 Brian 6
2 test1 Carl 7
3 test2 Amy 3
4 test2 Brian 2
5 test2 Carl 4
# Rename columns
result.rename(columns={'level_1':'name',
0: 'score'},inplace=True)
level_0 name score
0 test1 Amy 5
1 test1 Brian 6
2 test1 Carl 7
3 test2 Amy 3
4 test2 Brian 2
5 test2 Carl 4
# Set test values to index of DataFrame
result.index = result['level_0']
level_0 level_0 name score
test1 test1 Amy 5
test1 test1 Brian 6
test1 test1 Carl 7
test2 test2 Amy 3
test2 test2 Brian 2
test2 test2 Carl 4
# Rename index to blank
result.index.name=''
level_0 name score
test1 test1 Amy 5
test1 test1 Brian 6
test1 test1 Carl 7
test2 test2 Amy 3
test2 test2 Brian 2
test2 test2 Carl 4
# Drop test column from DataFrame
result.drop(columns=['level_0'],inplace=True)
name score
test1 Amy 5
test1 Brian 6
test1 Carl 7
test2 Amy 3
test2 Brian 2
test2 Carl 4
我想在 pandas 中将堆叠数据帧转换为常规数据帧。 原始df为:
Score | Amy | Brian | Carl |
---|---|---|---|
test1 | 5 | 6 | 7 |
test2 | 3 | 2 | 4 |
叠加后:
0 | ||
---|---|---|
test1 | Amy | 5 |
Brian | 6 | |
Carl | 7 | |
test2 | Amy | 3 |
Brian | 2 | |
Carl | 4 |
我想要的结果如下:
name | score | |
---|---|---|
test1 | Amy | 5 |
test1 | Brian | 6 |
test1 | Carl | 7 |
test2 | Amy | 3 |
test2 | Brian | 2 |
test2 | Carl | 4 |
如有任何建议,我们将不胜感激!
这比上面@Quang Hoang 的回答稍长,但也应该如此。它在堆叠 DataFrame
后拾取test1 Amy 5
test1 Brian 6
test1 Carl 7
test2 Amy 3
test2 Brian 2
test2 Carl 4
# Reset index to flatten DataFrame
result = df_stk.reset_index().copy()
level_0 level_1 0
0 test1 Amy 5
1 test1 Brian 6
2 test1 Carl 7
3 test2 Amy 3
4 test2 Brian 2
5 test2 Carl 4
# Rename columns
result.rename(columns={'level_1':'name',
0: 'score'},inplace=True)
level_0 name score
0 test1 Amy 5
1 test1 Brian 6
2 test1 Carl 7
3 test2 Amy 3
4 test2 Brian 2
5 test2 Carl 4
# Set test values to index of DataFrame
result.index = result['level_0']
level_0 level_0 name score
test1 test1 Amy 5
test1 test1 Brian 6
test1 test1 Carl 7
test2 test2 Amy 3
test2 test2 Brian 2
test2 test2 Carl 4
# Rename index to blank
result.index.name=''
level_0 name score
test1 test1 Amy 5
test1 test1 Brian 6
test1 test1 Carl 7
test2 test2 Amy 3
test2 test2 Brian 2
test2 test2 Carl 4
# Drop test column from DataFrame
result.drop(columns=['level_0'],inplace=True)
name score
test1 Amy 5
test1 Brian 6
test1 Carl 7
test2 Amy 3
test2 Brian 2
test2 Carl 4