如何在pandas中保留group by中的空白?
我需要对 pandas 中的 DataFrame 进行分组,但是当我这样做时,空值将转换为零,但我想保留空值。我不知道如何在熊猫中做到这一点。
输入:
Id Country Product sales qty price
1 Germany shoes 32 1 NaN
1 Germany shoes 32 1 2
2 England Shoes 22 1 NaN
2 England Shoes 22 1 NaN
3 Austria Shoes 0 3 NaN
3 Austria Shoes NaN NaN NaN
期望输出:
Id Country Product sales qty price
1 Germany shoes 64 2 2
2 England Shoes 44 2 NaN
3 Austria Shoes 0 3 NaN
I need to groupby
my DataFrame in pandas but when I am doing that the null values are converting into zero, but I want to retain nulls. I am not sure how to do that in pandas.
Input:
Id Country Product sales qty price
1 Germany shoes 32 1 NaN
1 Germany shoes 32 1 2
2 England Shoes 22 1 NaN
2 England Shoes 22 1 NaN
3 Austria Shoes 0 3 NaN
3 Austria Shoes NaN NaN NaN
Desired output:
Id Country Product sales qty price
1 Germany shoes 64 2 2
2 England Shoes 44 2 NaN
3 Austria Shoes 0 3 NaN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 < 中使用参数
min_count=1
代码>总和:Use parameter
min_count=1
insum
:您可以使用
isna
+group
+all
来mask
它相同的想法以不同的方式编写:
输出:
You could
mask
it usingisna
+group
+all
The same idea written differently:
Output: