如何与相同值合并DF的行
我正在使用python上的熊猫面临问题,但我无法解决。 我想合并/组合/重组具有相同URL的行。
编辑 : 我有一个像这样的数据框架:
url | col1 | col2 | col3 | col4 |
---|---|---|---|---|
aaA | xx | yy | ||
bbb | zz | |||
aaa | ee | |||
aa |
我想要这样的东西:
url | col1 col1 | col2 | col2 col3 | col3 col4 |
---|---|---|---|---|
aaa aaa | aaa aaa ee | xx | yy | |
bbb | zz cc | cc cc | ||
cc aa |
我尝试过使用Groupby,但是在我的df有没有URL的数据,我想保留它们。 我还尝试过与内部合并,这给了我不错的结果,但是我不知道为什么它会破坏DF内部的行数。
谢谢。
I am facing a problem using pandas on python and i can't solve it.
I would like to merge/combine/regroup the rows which have the same url.
EDIT :
I have a dataframe looking like this :
url | col1 | col2 | col3 | col4 |
---|---|---|---|---|
aaa | xx | yy | ||
bbb | zz | |||
aaa | ee | |||
AA |
I would like something like this :
url | col1 | col2 | col3 | col4 |
---|---|---|---|---|
aaa | ee | xx | yy | |
bbb | zz | cc | ||
AA |
I've tried using groupby, but in my df i've datas which don't have URL and i want to keep them.
I've also tried merge with inner, which gives me pretty good results but i don't know why it decuplates the number of rows inside my df.
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
groupby
和first
。You can use
groupby
andfirst
.我认为您应该使用
Groupby,Nunique和NP.Where
来解决此问题。请参阅以下有关此问题的讨论。
pandas-dataframe-check-check-check-frame-check-if-multiple-multiple-multiple-lows-有个性值
I think you should use
groupby, nunique, and np.where
to solve this issue.See the following discussion regarding this problem.
pandas-dataframe-check-if-multiple-rows-have-the-same-value
结果:
Result: