Python 创建列出值的透视表

Python 创建列出值的透视表,python,pandas,pivot-table,Python,Pandas,Pivot Table,我需要使用什么aggfunc来使用透视表生成列表?我试着使用str,但效果不太好 输入 import pandas as pd data = { 'Test point': [0, 1, 2, 0, 1], 'Experiment': [1, 2, 3, 4, 5] } df = pd.DataFrame(data) print df pivot = pd.pivot_table(df, index=['Test point'], values=['Experiment'],

我需要使用什么aggfunc来使用透视表生成列表?我试着使用str,但效果不太好

输入

import pandas as pd
data = {
    'Test point': [0, 1, 2, 0, 1],
    'Experiment': [1, 2, 3, 4, 5]
}
df = pd.DataFrame(data)
print df

pivot = pd.pivot_table(df, index=['Test point'], values=['Experiment'], aggfunc=len)
print pivot

pivot = pd.pivot_table(df, index=['Test point'], values=['Experiment'], aggfunc=str)
print pivot
   Experiment  Test point
0           1           0
1           2           1
2           3           2
3           4           0
4           5           1
            Experiment
Test point            
0                    2
1                    2
2                    1
                                                Experiment
Test point                                                
0           0    1\n3    4\nName: Experiment, dtype: int64
1           1    2\n4    5\nName: Experiment, dtype: int64
2                   2    3\nName: Experiment, dtype: int64
输出

import pandas as pd
data = {
    'Test point': [0, 1, 2, 0, 1],
    'Experiment': [1, 2, 3, 4, 5]
}
df = pd.DataFrame(data)
print df

pivot = pd.pivot_table(df, index=['Test point'], values=['Experiment'], aggfunc=len)
print pivot

pivot = pd.pivot_table(df, index=['Test point'], values=['Experiment'], aggfunc=str)
print pivot
   Experiment  Test point
0           1           0
1           2           1
2           3           2
3           4           0
4           5           1
            Experiment
Test point            
0                    2
1                    2
2                    1
                                                Experiment
Test point                                                
0           0    1\n3    4\nName: Experiment, dtype: int64
1           1    2\n4    5\nName: Experiment, dtype: int64
2                   2    3\nName: Experiment, dtype: int64
所需输出

            Experiment
Test point                                                
0           1, 4
1           2, 5
2           3
使用

或者,
groupby
也可以

In [1831]: df.groupby('Test point').agg({
                'Experiment': lambda x: x.astype(str).str.cat(sep=', ')})
Out[1831]:
           Experiment
Test point
0                1, 4
1                2, 5
2                   3
但是,如果需要,则作为列表

In [1861]: df.groupby('Test point').agg({'Experiment': lambda x: x.tolist()})
Out[1861]:
           Experiment
Test point
0              [1, 4]
1              [2, 5]
2                 [3]


x.astype(str).str.cat(sep=',')
','相似。join(x.astype(str))
选项1
str
Pre-conversion+
groupby
+
apply

您可以预转换为字符串以简化
groupby
调用

df.assign(Experiment=df.Experiment.astype(str))\
      .groupby('Test point').Experiment.apply(', '.join).to_frame('Experiment')

           Experiment
Test point           
0                1, 4
1                2, 5
2                   3
对此的修改将涉及就地分配,因为速度(
assign
返回副本,速度较慢):

还有修改原始数据帧的缺点

性能

# Zero's 1st solution
%%timeit
df.groupby('Test point').agg({'Experiment': lambda x: x.astype(str).str.cat(sep=', ')})

100 loops, best of 3: 3.72 ms per loop
这个的就地版本与上面的相同

# proposed in this post
%%timeit -n 1
df.Experiment = df.Experiment.astype(str)
df.groupby('Test point').agg({'Experiment' : ', '.join})

1 loop, best of 3: 2.21 ms per loop
#在本文中提出
%%timeit-n1
df.Experiment=df.Experiment.astype(str)
groupby('testpoint').agg({'experience':','.join})
1圈,最佳3圈:每圈2.21毫秒

对于较大的数据帧,
agg
应该比
应用
的速度提高。

您可以将
列表
本身用作一个函数:

>>> pd.pivot_table(df, index=['Test point'], values=['Experiment'], aggfunc=lambda x:list(x))
           Experiment
Test point           
0              [1, 4]
1              [2, 5]
2                 [3]
df.assign(Experiment=df.Experiment.astype(str))\
         .groupby('Test point').agg({'Experiment' : ', '.join})

           Experiment
Test point           
0                1, 4
1                2, 5
2                   3
# proposed in this post
%%timeit -n 1
df.Experiment = df.Experiment.astype(str)
df.groupby('Test point').agg({'Experiment' : ', '.join})

1 loop, best of 3: 2.21 ms per loop
>>> pd.pivot_table(df, index=['Test point'], values=['Experiment'], aggfunc=lambda x:list(x))
           Experiment
Test point           
0              [1, 4]
1              [2, 5]
2                 [3]