熊猫合并并完成相同ID的行
Here is an extract of my dataframe :
ID | LU | MA | ME | JE | VE | SA | DI |
---|---|---|---|---|---|---|---|
200 | B | B | B | ||||
201 | C | C | C | C | C | ||
211 | A | ||||||
211 | D | D | D | ||||
211 | B | ||||||
213 | A | A | |||||
216 | K | K | K | K | |||
216 | K | ||||||
217 | B | B | B | B | B |
我有一些与相同的id
的行,并希望在完成它们时将它们“合并”到一行中。 Here is an example of what I want to have as a result :
ID | LU | MA | ME | JE | VE | SA | DI |
---|---|---|---|---|---|---|---|
200 | B | B | B | ||||
201 | C | C | C | C | C | ||
211 | A | D | D | D | B | ||
213 | A | A | |||||
216 | K | K | K | K | K | ||
217 | B | B | B | B | B |
我是Pandas DataFrames的新手,并且尝试使用drop_duplicates
方法,但是我需要一些不同的东西,因为keep> keep
参数的限制。 同样,数据帧由ID排序。
Here is an extract of my dataframe :
ID | LU | MA | ME | JE | VE | SA | DI |
---|---|---|---|---|---|---|---|
200 | B | B | B | ||||
201 | C | C | C | C | C | ||
211 | A | ||||||
211 | D | D | D | ||||
211 | B | ||||||
213 | A | A | |||||
216 | K | K | K | K | |||
216 | K | ||||||
217 | B | B | B | B | B |
I have some rows with same ID
and want to "merge" them into only one row while completing them.
Here is an example of what I want to have as a result :
ID | LU | MA | ME | JE | VE | SA | DI |
---|---|---|---|---|---|---|---|
200 | B | B | B | ||||
201 | C | C | C | C | C | ||
211 | A | D | D | D | B | ||
213 | A | A | |||||
216 | K | K | K | K | K | ||
217 | B | B | B | B | B |
I'm new to pandas dataframes and have try to use drop_duplicates
method but I need something different because of the restriction on keep
parameters.
Also the dataframe is sorted by ID.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果每个组只有一个非空值使用:
如果可能的多个值,并且需要在原始顺序中使用唯一值,则使用lambda函数:
If there is only one non empty value per groups use:
If possible multiple values and need unique values in original order use lambda function:
这可以视为枢轴。您需要先融化DF然后旋转:
This could be treated as a pivot. You'd need to melt the df first then pivot: