Home > front end >  split content of a column pandas
split content of a column pandas

Time:04-27

I have the following Pandas Dataframe

enter image description here

Which can also be generated using this list of dictionaries:

list_of_dictionaries = [
    {'Project': 'A', 'Hours': 2, 'people_ids': [16986725, 17612732]},
    {'Project': 'B', 'Hours': 2, 'people_ids': [17254707, 17567393, 17571668, 17613773]},
    {'Project': 'C', 'Hours': 3, 'people_ids': [17097009, 17530240, 17530242, 17543865, 17584457, 17595079]},
    {'Project': 'D', 'Hours': 2, 'people_ids': [17097009, 17584457, 17702185]}]

I have implemented kind of what I need, but adding columns vertically:

df['people_id1']=[x[0] for x in df['people_ids'].tolist()]
df['people_id2']=[x[1] for x in df['people_ids'].tolist()]

And then I get a different column of every single people_id, just until the second element, because when I add the extraction 3rd element on a third column, it crashes because , there is no 3rd element to extract from the first row.

enter image description here

Even though, what I am trying to do is to extract every people_id from people_ids column, and then each one of those will have their associated value from the Project and Hours columns, so I get a dataset like this one:

enter image description here

Any idea on how could I get this output?

CodePudding user response:

I think what you are looking for is explode on 'people_ids' column.

df = df.explode('people_ids', ignore_index=True)
  • Related