[英]Dataframe with a column which every row is a list of values
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.提前感谢您的回答。
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
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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.