Processing/Transposing Pandas 数据框

Processing/Transposing Pandas Dataframe

我得到以下 pandas 数据框:

Id    Category
1     type 2
1     type 3
1     type 2
2     type 1
2     type 2

我需要将上述数据帧处理并转置为:

Id Category_type_1 Category_type_2 Category_type_3
1          0               2              1
2          1               1              0

如果有人能在 python 中展示最简单的编码方法,我们将不胜感激。

pd.crosstab(df['Id'], df['Category'])
Out: 
Category  type 1  type 2  type 3
Id                              
1              0       2       1
2              1       1       0

使用pivot_table:

print (df.pivot_table(index='Id', columns='Category', aggfunc=len, fill_value=0))
Category  type 1  type 2  type 3
Id                              
1              0       2       1
2              1       1       0

时间:

Small DataFrame - len(df)=5:

In [63]: %timeit df.groupby(df.columns.tolist()).size().unstack().fillna(0)
1000 loops, best of 3: 1.33 ms per loop

In [64]: %timeit (df.pivot_table(index='Id', columns='Category', aggfunc=len, fill_value=0))
100 loops, best of 3: 3.77 ms per loop

In [65]: %timeit pd.crosstab(df['Id'], df['Category'])
100 loops, best of 3: 4.82 ms per loop

Large DataFrame - len(df)=5k:

df = pd.concat([df]*1000).reset_index(drop=True)

In [59]: %timeit df.groupby(df.columns.tolist()).size().unstack().fillna(0)
1000 loops, best of 3: 1.73 ms per loop

In [60]: %timeit (df.pivot_table(index='Id', columns='Category', aggfunc=len, fill_value=0))
100 loops, best of 3: 4.64 ms per loop

In [61]: %timeit pd.crosstab(df['Id'], df['Category'])
100 loops, best of 3: 5.46 ms per loop

Very large DataFrame - len(df)=5m:

df = pd.concat([df]*1000000).reset_index(drop=True)

In [55]: %timeit df.groupby(df.columns.tolist()).size().unstack().fillna(0)
1 loop, best of 3: 514 ms per loop

In [56]: %timeit (df.pivot_table(index='Id', columns='Category', aggfunc=len, fill_value=0))
1 loop, best of 3: 907 ms per loop

In [57]: %timeit pd.crosstab(df['Id'], df['Category'])
1 loop, best of 3: 822 ms per loop

我会 groupby 并使用 size

df.groupby(df.columns.tolist()).size().unstack().fillna(0)