简体   繁体   中英

python pandas groupby: drop column that was used for grouping

I have a dataframe

print(my_df)
   A    B    C
0  1   20  0.1
1  1   30  0.2
2  1   40  0.3
3  2  200  0.7
4  2  300  0.8
5  2  400  0.9

that I group by column 'A'

grouped = my_df.groupby('A')

that I transform into a list:

grouped.apply(pd.Series.tolist)

A
1    [[1.0, 20.0, 0.1], [1.0, 30.0, 0.2], [1.0, 40....
2    [[2.0, 200.0, 0.7], [2.0, 300.0, 0.8], [2.0, 4...

However, I would like to get rid of the now redundant column 'A' values, to have

A
1    [[20.0, 0.1], [30.0, 0.2], [40....
2    [[200.0, 0.7], [300.0, 0.8], [4...

the grouped object contains all three columns:

grouped.obj.columns
Index(['A', 'B', 'C'], dtype='object')

but I cannot drop any of them:

grouped.drop('A')

AttributeError: Cannot access callable attribute 'drop' of 'DataFrameGroupBy' objects, try using the 'apply' method

how can I use drop with apply?

grouped.apply(pd.DataFrame.drop( ??? 

Thank you

Edit:

More concretely I am doing

grouped.apply(pd.Series.tolist).tolist()

with this I obtain

[[[1.0, 20.0, 0.1], [1.0, 30.0, 0.2], [1.0, 40.0, 0.3]], [[2.0, 200.0, 0.7], [2.0, 300.0, 0.8], [2.0, 400.0, 0.9]]]

but I would like to have

[[[20.0, 0.1], [30.0, 0.2], [40.0, 0.3]], [[200.0, 0.7], [300.0, 0.8], [400.0, 0.9]]]

Try the below code, hope this will help:

df.set_index('A',inplace=True)
df.groupby('A').apply(pd.Series.tolist)

Ouput will be:

A
1       [[20.0, 0.1], [30.0, 0.2], [40.0, 0.3]]
2    [[200.0, 0.7], [300.0, 0.8], [400.0, 0.9]]
dtype: object

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