-2

I'm looking to combine pandas rows in a Dataframe by matching userID into a list. There are 1000+ userIDs and multiple entries for each.

I want one row for each user. I found a thread that does the complete opposite of what I am trying to do, which is: How to explode a list inside a Dataframe cell into separate rows Thanks in advance, guys.

what would need to be done for a rows with string and number?

  userID    alcohol 
  U1001     No_Alcohol_Served 7.0
            Wine-Beer 2.0
  U1002     Full_Bar 1.0
            No_Alcohol_Served 3.0
            Wine-Beer 6.0
  U1003     Full_Bar 2.0
            No_Alcohol_Served 8.0
            Wine-Beer 3.0
  U1004    No_Alcohol_Served 4.0
           Wine-Beer 4.0

I would like to say something like:

U1001 : No_Alcohol_served:7.0, Wine-Beer:2.0
U1002 : Full_Bar:1.0, No_Alcohol_served:3.0, Wine_beer:6.0

so on and so forth

Tuxedo
  • 1
  • 1

1 Answers1

1

You could try something like this:

df.groupby('userID').apply(lambda x: x['name'].tolist())

Example:

Given a df:

  userID name
0  U1001    a
1  U1001    b
2  U1001    c
3  U1002    d
4  U1002    e
5  U1003    f

>>> df.groupby('userID').apply(lambda x: x['name'].tolist())
userID
U1001    [a, b, c]
U1002       [d, e]
U1003          [f]
dtype: object
sacuL
  • 49,704
  • 8
  • 81
  • 106