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

标签 pandas

我有一个 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/

相关文章:

python - Python pandas 关于重新索引 bool 系列键的用户警告

python - 使用 Pandas 如何找到满足条件的值的计数?

python - 将相同的 Pandas 数据帧存储到数据库后不相等

python - Pandas 显示列号

python - 我可以更有效地拆分包含混合元组/无的列吗?

python - 与 Pandas 的音量叠加

python - 在 Pandas 中创建百分位桶

python - 将数据集从宽 pandas 转换为长 pandas

python - 如何将 timedelta 转换为 Pandas 中的时间?

pandas - 如何按 'pandas' 中的列获取缺失/NaN 数据的汇总计数?