简体   繁体   中英

pandas stacking a dataframe

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

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

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

EDIT:

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

If sensorId is column:

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

Or use:

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

And also a little correction to @jezrael's (his is nice tho):

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

And now:

print(df)

Is:

                  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM