按组从Pandas Dataframe中删除`NaN`

3

I have a Dataframe that looks like this:

value   attribute1  attribute2  attribute3  attribute4
value1  NaN         NaN         foo         NaN
value1  bar         NaN         NaN         NaN
value1  NaN         baz         NaN         NaN
value1  NaN         NaN         NaN         boo

我正在尝试找到一种方法来压缩框架,使其看起来更像这样:
value   attribute1  attribute2  attribute3  attribute4
value1  bar         baz         foo         boo

我还没有找到任何特定的解决方案,可以让我做到这一点。


你的 value 列会有多个值吗? - jpp
@jpp 是的,实际的DF比我上面展示的要大得多。 - Token Joe
3个回答

3
In [166]: df.groupby('value').first()
Out[166]:
       attribute1 attribute2 attribute3 attribute4
value
value1        bar        baz        foo        boo

或者

In [167]: df.groupby('value', as_index=False).first()
Out[167]:
    value attribute1 attribute2 attribute3 attribute4
0  value1        bar        baz        foo        boo

哇,太棒了!第一个似乎重复了值,但第二个选项非常好,非常感谢! - Token Joe

2
df.bfill().iloc[[0]]
Out[201]: 
    value attribute1 attribute2 attribute3 attribute4
0  value1        bar        baz        foo        boo

嘿,这也非常好用,谢谢! - Token Joe

1
一种方法是使用 sort_values,然后使用 bfilldrop_duplicates
即使 value 列中有多个唯一值且未排序,此方法也适用。
res = df.sort_values('value')\
        .bfill()\
        .drop_duplicates('value')

#     value attribute1 attribute2 attribute3 attribute4
# 0  value1        bar        baz        foo        boo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接