Skip to content Skip to sidebar Skip to footer

Pandas: How Do I Loop Through And Remove Rows Where A Column Has A Single Entry

So I have a pandas dataframe with some number of columns (Below is the code for a simple dataframe but the real dataframe has over 100 columns): X = pd.DataFrame([['A','Z'],['A','Z

Solution 1:

You can use duplicated as so:

X = pd.DataFrame([["A","Z"],["A","Z"],["B","Z"], ["A","Y"]],columns=["COL1","COL2"])

forcolumnin X:
    X = X[X[column].duplicated(keep=False)]

Output:

  COL1 COL2
0A    Z
1A    Z

Solution 2:

This is to fix your code

X[X.groupby('COL1').COL2.transform('count')>1]
  COL1 COL2
0A    Z
1A    Z

Usually I will do duplicated

X[X.COL1.duplicated(keep=False)]
  COL1 COL2
0A    Z
1A    Z

Post a Comment for "Pandas: How Do I Loop Through And Remove Rows Where A Column Has A Single Entry"