python-3.x - 带有熊猫的列中的行内换行
问题描述
大熊猫有什么方法可以将列中的一行内的数据分开吗?行有多个数据,我的意思是,我按 col1 分组,结果是我有一个这样的 df:
col1 Col2
0 1 abc,def,ghi
1 2 xyz,asd
and desired output would be:
Col1 Col2
0 1 abc
def
ghi
1 2 xyz
asd
谢谢
解决方案
使用str.split
和explode
:
print (df.assign(Col2=df["Col2"].str.split(","))
.explode("Col2"))
col1 Col2
0 1 abc
0 1 def
0 1 ghi
1 2 xyz
1 2 asd
推荐阅读
- python - 在网站的所有页面上维护 aws lex 聊天通信
- javascript - 需要帮助来检查 Js 中井字游戏的获胜条件
- jquery - 从另一个页面滚动到主页上的锚点
- ruby-on-rails - 如何更新依赖于强参数的值
- python - 请解释python中'%'的用法
- c# - 加载项未在 Outlook 添加列表部分中列出
- python - 使用 random.choice 从文件中的随机行打印两个单词
- powershell - 有没有更好的方法来获取给定 AD 组 ObjectGUID 列表的 AD 组成员?
- amazon-web-services - 如何从头开始在云实例中部署 Java 应用程序到高级架构?
- java - Java SoftReference 奇怪的行为