Search code examples
pythonpandasdata-sciencedata-analysissklearn-pandas

How to remove rows in a column with certain value in Excel file with Python


I have data like this:

userdata

I want to remove the rows in user ID_2 column which the data is more than and less than 5 digit


Solution

  • Since the question is

    which the data is more than and less than 5 digit

    Which means only 5 digits values are required, I slightly modified @ChrisA comment to fit the requirement:

    df = pd.DataFrame({'userID': [20394756382,29304857203,20294857642,20293847564,20192837453],
                       'UserID2' : [38493,2212324,30498,30928,432]})
    
    df = df.loc[df['UserID2'].astype(str).str.len().eq(5)]
    print(df)
            userID  UserID2
    0  20394756382    38493
    2  20294857642    30498
    3  20293847564    30928