Home > database >  Dataframe with a column which every row is a list of values
Dataframe with a column which every row is a list of values

Time:05-25

I have a pandas dataframe like this:

         name     favourite_fruits                     votes
0         A          banana                       [5, 0, 5, 0, 5]
1         B          orange                          [5, 0, 5]
2         C          peach                              [5]

I need to convert "votes" column in:

         name     favourite_fruits                     vote1 vote2 vote3 vote4 vote5
0         A          banana                              5     0     5     0     5
1         B          orange                              5     0     5
2         C          peach                               5

How i can do that? Thanks in advance for your answers.

CodePudding user response:

Try:

#explode to split list into individual rows
df = df.explode("votes")

#groupby and cumcount to get the total votes per index
df["column"] = df.groupby(level=0).cumcount().add(1)

#pivot to get the expected output
output = df.pivot(["name","favourite_fruits"],"column","votes").add_prefix("vote").rename_axis(None,axis=1).reset_index()

>>> output
  name favourite_fruits vote1 vote2 vote3 vote4 vote5
0    A           banana     5     0     5     0     5
1    B           orange     5     0     5   NaN   NaN
2    C            peach     5   NaN   NaN   NaN   NaN

CodePudding user response:

Here's an alternative approach:

result = pd.concat(
    [df[["name", "favourite_fruits"]],
     pd.DataFrame(lst for lst in df["votes"])
       .rename(columns=lambda n: f"vote{n   1}")],
    axis=1
)
  • Related