简体   繁体   中英

Is there a way to groupby multiple columns under a single column in pandas?

I have a table like this ---

AREA COL1 COL2 COL3
Dhaka [1,2,3] [2,3] [4,5,6]
Sylhet [10,11] [7,9,10] [10,13,7]

I want this table in this shape--

Area total_num
Dhaka 1
2
3
4
5
6
Sylhet 10
11
7
9
13

how can I do it in pandas?

If necessary convert values to lists:

import ast

df.iloc[:, 1:] = df.iloc[:, 1:].applymap(ast.literal_eval)

And then reshape by DataFrame.set_index with DataFrame.stack , explode lists by Series.explode , creare 2 columns DataFrame and last remove duplicates:

df = (df.set_index('AREA')
        .stack()
        .explode()
        .droplevel(1)
        .reset_index(name='total_num')
        .drop_duplicates())
print (df)
      AREA total_num
0    Dhaka         1
1    Dhaka         2
2    Dhaka         3
5    Dhaka         4
6    Dhaka         5
7    Dhaka         6
8   Sylhet        10
9   Sylhet        11
10  Sylhet         7
11  Sylhet         9
14  Sylhet        13

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