gpt4 book ai didi

pandas - 将 pandas 列表转换为虚拟变量

转载 作者:行者123 更新时间:2023-12-02 04:40:21 24 4
gpt4 key购买 nike

我有一个 pandas dataFrame,其中包含我想要转换为虚拟变量的变量列表。基本上我想转换:

enter image description here

对此:

enter image description here

最佳答案

df = pd.DataFrame({0: [['hello', 'motto'], ['motto', 'mania']]})
print(df)

0
0 [hello, motto]
1 [motto, mania]

使用str.join接下来是 str.get_dummies

df[0].str.join('|').str.get_dummies()

hello mania motto
0 1 0 1
1 0 1 1

关于pandas - 将 pandas 列表转换为虚拟变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43596570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com