Python Forum
how to combine rows to a column base on ids
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to combine rows to a column base on ids
#1
I have a dataframe:
df = pd.DataFrame({'id': [1,1,1,1,2,2,2,3,3,3,3,3],
                   'cost': [3,4,5,6,7,8,9,11,15,10,9,7],
                   'amount': [23,34,45,26,27,38,39,611,615,610,89,79]
                 })
df
I want a df with 3 rows and 3 columns:
id cost_arr amount_arr
1 [3,4,5,6] [23,34,45,26]
2 [7,8,9] [27,38,39]
3 [11,15,10,9,7] [611,615,610,89,79]

cost_arr and amount_arr are arrays.
How can I do that?
Thanks
Jeff
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 797 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to combine multiple column values into 1? cubangt 15 2,626 Aug-11-2022, 08:25 PM
Last Post: cubangt
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,599 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  How to combine multiple rows of strings into one using pandas? shantanu97 1 3,096 Aug-22-2021, 05:26 AM
Last Post: klllmmm
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,080 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,957 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  How to filter out Column data From Multiple rows data? firaki12345 10 5,008 Feb-06-2021, 04:54 AM
Last Post: buran
  How to generate rows based on values in a column to fill missing values codesmatter 1 2,093 Oct-31-2020, 12:05 AM
Last Post: Larz60+
  How to diff pandas rows and modify column value Mekala 1 1,654 Sep-18-2020, 12:38 PM
Last Post: Mekala
  Column to rows kmrashish1985 0 1,276 Aug-20-2020, 09:15 PM
Last Post: kmrashish1985

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020