根据其他列值删除重复项 (Python)

时间:2021-01-21 22:57:59

标签: python pandas

我有一个包含 3 列的数据框。我想根据其他列中的值删除 A 列中的重复项。我不知疲倦地搜索,找不到这样的解决方案。

示例:

<头>
A B C
Family1
Family1 1234
Family1 1245
Family1 3456 78787
Family2
Family3

基本上我只想在其余列都是 nan 时删除重复项。否则,副本可以保留。

期望输出:

<头>
A B C
Family1 1234
Family1 1245
Family1 3456 78787
Family2
Family3

Family2 和 Family3 保留在 df 中,因为它们没有重复项,即使两列都是 nan

2 个答案:

答案 0 :(得分:3)

你说的不是很清楚。如果 B 列和 C 列都是 NaN,我怀疑您想删除 A 列中的任何重复项。如果是这样,请尝试;

df[~(df.A.duplicated(keep=False)&(df.B.isna()&df.C.isna()))]

答案 1 :(得分:3)

尝试一个双布尔值,这对于所有重复项返回真,对于 ['A'] 之后的任何列都是空值,这将返回真。如果这两个条件都满足,我们将使用 class LabeledCheckbox extends StatelessWidget { const LabeledCheckbox({ this.label, this.contentPadding, this.value, this.onTap, this.activeColor, this.fontSize, this.gap = 4.0, this.bold = false, }); final String label; final EdgeInsets contentPadding; final bool value; final Function onTap; final Color activeColor; final double fontSize; final double gap; final bool bold; @override Widget build(BuildContext context) { return GestureDetector( onTap: () => onTap(!value), child: Padding( padding: contentPadding ?? const EdgeInsets.all(0), child: Row( mainAxisSize: MainAxisSize.min, children: <Widget>[ Checkbox( value: value, activeColor: activeColor, visualDensity: VisualDensity.compact, onChanged: (val) => onTap(val), ), SizedBox( width: gap, ), // you can control gap between checkbox and label with this field Flexible( child: Text( label, style: TextStyle( fontSize: fontSize, fontWeight: bold ? FontWeight.bold : FontWeight.normal, ), ), ), ], ), ), ); } } 运算符将其排除,该运算符反转布尔值。

~