Pandas:在列表的列上旋转?

2024-10-04 05:21:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Pandas中有这样一个数据框:

                      Age InterventionType PrimaryPurpose
0         [Adult, Senior]           Device    Treatment
1  [Child, Adult, Senior]             Drug    Basic Science
2         [Adult, Senior]             Drug    Treatment
3         [Adult, Senior]              NaN            NaN
4         [Adult, Senior]            Other            NaN

想以InterventionType为中心,这样我得到:

^{pr2}$

我该怎么做?另外,我的数据帧有列表不是标准的吗?如果是这样,什么是一个好的做法“取消名单”的名单?在


Tags: 数据childpandasagebasicdevicenanscience
1条回答
网友
1楼 · 发布于 2024-10-04 05:21:50

假设您的数据帧名为df,可以执行以下操作:

pd.DataFrame(
    df["Age"].values.tolist(),
    index=df["InterventionType"]
).stack().reset_index().drop("level_1", axis=1).groupby(["InterventionType", 0]).size()

将返回以下内容

^{pr2}$

这是一个多指标的熊猫系列。上面代码中的索引名是“InterventionType”和0,但是这些名称可以很容易地更改。在

相关问题 更多 >