我有一个带有数据行的pandas DataFrame ::
["abc", "xyz"]
["cde", "fgh"]
["xyz"]
即相同的objectID通常有多个条目,但有时/通常这些条目具有NA。
因此,我只是在寻找一种可以结合ObjectID并报告非NA条目的方法,例如上面的内容可以归结为:
# objectID grade OS method
object_id_0001 AAA Mac organic
object_id_0001 AAA Mac NA
object_id_0001 AAA NA organic
object_id_0002 NA NA NA
object_id_0002 ABC Win NA
答案 0 :(得分:4)
这有效并且已经存在很长时间了。但是,有人声称这是一个可能已修复的错误。按照目前的实现,first
返回第一个非空元素(如果每列存在)。
df.groupby('objectID', as_index=False).first()
objectID grade OS method
0 object_id_0001 AAA Mac organic
1 object_id_0002 ABC Win NaN
pd.concat
pd.concat([
pd.DataFrame([d.lookup(d.notna().idxmax(), d.columns)], columns=d.columns)
for _, d in df.groupby('objectID')
], ignore_index=True)
objectID grade OS method
0 object_id_0001 AAA Mac organic
1 object_id_0002 ABC Win NaN
stack
df.set_index('objectID').stack().groupby(level=[0, 1]).head(1).unstack()
grade OS method
objectID
object_id_0001 AAA Mac organic
object_id_0002 ABC Win None
如果碰巧是字符串('NA'
)
df.mask(df.astype(str).eq('NA')).groupby('objectID', as_index=False).first()
答案 1 :(得分:2)
另一种更机械的方式
<button class="accordion" onclick="Hide()">show or hide</button>
<div class="inner" style="display:none">
What I want to show-hide
<button class="accordion" onclick="Hide">hide</button>
</div>
<script
function Hide()
{
var elements = document.getElementsByClassName("inner");
var requiredElement = elements[0];
var style = requiredElement.getAttribute("style");
if(style != "display:none")
requiredElement.setAttribute('style', 'display:none');
}
}</script>
答案 2 :(得分:2)
这可以bfill
+ drop_duplicates
df.groupby('objectID',as_index=False).bfill().drop_duplicates('objectID')
Out[939]:
objectID grade OS method
0 object_id_0001 AAA Mac organic
3 object_id_0002 ABC Win NaN