Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在熊猫中聚合数据?_Python_Python 3.x_Python 2.7_Pandas_Dataframe - Fatal编程技术网

Python 如何在熊猫中聚合数据?

Python 如何在熊猫中聚合数据?,python,python-3.x,python-2.7,pandas,dataframe,Python,Python 3.x,Python 2.7,Pandas,Dataframe,我有这个数据框: 为了澄清原因,以下是我在Excel中的真实数据集示例: . “Gimnasa”列既可以是索引,也可以是正常列,尽管我已经把它当作索引。“Plancha”和“Staddler”是练习栏,快照中还隐藏了17个练习栏。“Grupo”和“Sexo”分别是“Group”和“Sex” 该数据框架需要“分类”,以便为每次练习绘制一个群集图。这将是我需要的一个例子: Excel中的示例: 我遵循了下一个教程: 然而,由于这两个例子之间的差异,我被卡住了 我已将我的原始df一分为二,以便使用以下

我有这个数据框:

为了澄清原因,以下是我在Excel中的真实数据集示例: . “Gimnasa”列既可以是索引,也可以是正常列,尽管我已经把它当作索引。“Plancha”和“Staddler”是练习栏,快照中还隐藏了17个练习栏。“Grupo”和“Sexo”分别是“Group”和“Sex”

该数据框架需要“分类”,以便为每次练习绘制一个群集图。这将是我需要的一个例子:

Excel中的示例:

我遵循了下一个教程: 然而,由于这两个例子之间的差异,我被卡住了

我已将我的原始df一分为二,以便使用以下代码简化流程(或者我认为是这样):

df = pd.read_csv("data.csv",index_col=0)
df2 = df[['Group','Sex']]
df3 = df.drop(df[['Group','Sex']],axis=1)
series = df3.stack()
因此,我们有df2只包含Group和Sex列,df3包含所有练习及其分数,这两个数据框都以“Gimnasta”列作为索引,以便以后很容易识别每个gimnast的值


我不知道如何继续

同意比尔的观点,我们还有点猜测。 我认为最终使用.melt()的方法会奏效

data = {u'Exercise A': {0: 1L, 1: 2L, 2: 3L},
 u'Exercise B': {0: 4L, 1: 5L, 2: 5L},
 u'Group': {0: u'A', 1: u'B', 2: u'C'},
 u'Person': {0: u'Person1', 1: u'Person2', 2: u'Person1'},
 u'Sex': {0: u'F', 1: u'F', 2: u'M'}}

df = pd.DataFrame(data)

    Exercise A  Exercise B  Group   Person  Sex
0   1           4           A       Person1 F
1   2           5           B       Person2 F
2   3           5           C       Person1 M


df = pd.melt(df, id_vars=['Person','Group','Sex'], value_vars=['Exercise A', 'Exercise B'],var_name='Exercise', value_name='Score')
df = df.sort_values(['Person','Group'])


    Person  Group   Sex Exercise    Score
0   Person1 A       F   Exercise A  1
3   Person1 A       F   Exercise B  4
2   Person1 C       M   Exercise A  3
5   Person1 C       M   Exercise B  5
1   Person2 B       F   Exercise A  2
4   Person2 B       F   Exercise B  5

如果您希望使用.stack()路径


您可以从这里重置_index()。

我猜您的示例与
df
数据集的外观不完全相同。您能否提供生成
df
的代码。看见
df = pd.read_csv("data.csv",index_col=0)
df2 = df[['Group','Sex']]
df3 = df.drop(df[['Group','Sex']],axis=1)
series = df3.stack()
data = {u'Exercise A': {0: 1L, 1: 2L, 2: 3L},
 u'Exercise B': {0: 4L, 1: 5L, 2: 5L},
 u'Group': {0: u'A', 1: u'B', 2: u'C'},
 u'Person': {0: u'Person1', 1: u'Person2', 2: u'Person1'},
 u'Sex': {0: u'F', 1: u'F', 2: u'M'}}

df = pd.DataFrame(data)

    Exercise A  Exercise B  Group   Person  Sex
0   1           4           A       Person1 F
1   2           5           B       Person2 F
2   3           5           C       Person1 M


df = pd.melt(df, id_vars=['Person','Group','Sex'], value_vars=['Exercise A', 'Exercise B'],var_name='Exercise', value_name='Score')
df = df.sort_values(['Person','Group'])


    Person  Group   Sex Exercise    Score
0   Person1 A       F   Exercise A  1
3   Person1 A       F   Exercise B  4
2   Person1 C       M   Exercise A  3
5   Person1 C       M   Exercise B  5
1   Person2 B       F   Exercise A  2
4   Person2 B       F   Exercise B  5
data = {u'Exercise A': {0: 1L, 1: 2L, 2: 3L},
 u'Exercise B': {0: 4L, 1: 5L, 2: 5L},
 u'Group': {0: u'A', 1: u'B', 2: u'C'},
 u'Person': {0: u'Person1', 1: u'Person2', 2: u'Person1'},
 u'Sex': {0: u'F', 1: u'F', 2: u'M'}}

df = pd.DataFrame(data)

    Exercise A  Exercise B  Group   Person  Sex
0   1           4           A       Person1 F
1   2           5           B       Person2 F
2   3           5           C       Person1 M


df = df.set_index(['Person','Sex','Group'])
df.stack()

Person   Sex  Group            
Person1  F    A      Exercise A    1
                     Exercise B    4
Person2  F    B      Exercise A    2
                     Exercise B    5
Person1  M    C      Exercise A    3
                     Exercise B    5
dtype: int64

which is a series with a 4 level multi_index() where the inner level is 'None'.

names = [x for x in  df.index.names]
names[-1] ='Exercise'
df.index.names = names
df.to_frame('Score')

                                Score
Person  Sex Group   Exercise    
Person1 F   A       Exercise A  1
                    Exercise B  4
Person2 F   B       Exercise A  2
                    Exercise B  5
Person1 M   C       Exercise A  3
                    Exercise B  5