本文介绍了 pandas 堆叠数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,看起来像:

I have a dataframe, that Looks like:

sensorId                   1      2      3   

9b:f3:55:19:00:4b:12:00    1      7      8    
bf:f3:55:19:00:4b:12:00    6      5      9   
da:f3:55:19:00:4b:12:00    1      1      2  

我想重组为具有以下结构的数据帧:

and I want to restructure into a dataframe with the following struture:

 sensorId                 y
 9b:f3:55:19:00:4b:12:00  1
 9b:f3:55:19:00:4b:12:00  7
 9b:f3:55:19:00:4b:12:00  8
 bf:f3:55:19:00:4b:12:00  6
 bf:f3:55:19:00:4b:12:00  5
 bf:f3:55:19:00:4b:12:00  9
 da:f3:55:19:00:4b:12:00  1
 da:f3:55:19:00:4b:12:00  1
 da:f3:55:19:00:4b:12:00  2

我尝试使用 df.stack(),但结果并不乐观,因为它返回一个 pd.series:

I tried using df.stack(), but the result isn't promising, because it Returns a pd.series:

9b:f3:55:19:00:4b:12:00  1      1
                         2      7
                         3      8
bf:f3:55:19:00:4b:12:00  1      6
                         2      5
                         3      9
da:f3:55:19:00:4b:12:00  1      1
                         2      1
                         3      2

推荐答案

使用 double Series.reset_index - 第一个用于删除 MultiIndex 的第二级,第二个用于转换 SeriesDataFrame:

Use double Series.reset_index - first for remove second level of MultiIndex and second for convert Series to DataFrame:

df = df.stack().reset_index(level=1, drop=True).reset_index(name='y')
print (df)
                  sensorId  y
0  9b:f3:55:19:00:4b:12:00  1
1  9b:f3:55:19:00:4b:12:00  7
2  9b:f3:55:19:00:4b:12:00  8
3  bf:f3:55:19:00:4b:12:00  6
4  bf:f3:55:19:00:4b:12:00  5
5  bf:f3:55:19:00:4b:12:00  9
6  da:f3:55:19:00:4b:12:00  1
7  da:f3:55:19:00:4b:12:00  1
8  da:f3:55:19:00:4b:12:00  2

因为df.stack返回MultiIndex,说明sensorId不是列,而是索引.

Because df.stack return MultiIndex, it means sensorId is not column, but index.

如果 sensorId 是列:

df = df.set_index('sensorId').stack().reset_index(level=1, drop=True).reset_index(name='y')

这篇关于 pandas 堆叠数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 16:13