How to conditionally remove duplicates from a pandas dataframe

后端 未结 3 2118
梦如初夏
梦如初夏 2021-01-02 02:41

Consider the following dataframe

import pandas as pd
df = pd.DataFrame({\'A\' : [1, 2, 3, 3, 4, 4, 5, 6, 7],
                   \'B\' : [\'a\',\'b\',\'c\',\'         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 03:06

    Or you can just using first(), by using the first , will give back the first notnull value, so the order of original input does not really matter.

    df.groupby(['A','B']).first()
    
    Out[180]: 
        Col_1  Col_2
    A B             
    1 a   NaN      2
    2 b     A      2
    3 c     A      3
    4 d     B      3
    5 e     B      4
    6 f   NaN      4
    7 g   NaN      5
    

提交回复
热议问题