带有一列的数据框,每一行都是一个值列表
Dataframe with a column which every row is a list of values
我有一个这样的 pandas 数据框:
name favourite_fruits votes
0 A banana [5, 0, 5, 0, 5]
1 B orange [5, 0, 5]
2 C peach [5]
我需要转换以下的“投票”列:
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
我该怎么做?
预先感谢您的回答。
尝试:
#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
这是另一种方法:
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
)
我有一个这样的 pandas 数据框:
name favourite_fruits votes
0 A banana [5, 0, 5, 0, 5]
1 B orange [5, 0, 5]
2 C peach [5]
我需要转换以下的“投票”列:
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
我该怎么做? 预先感谢您的回答。
尝试:
#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
这是另一种方法:
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
)