Search code examples
pythonfilterdatasetdata-cleaning

Subset the dataframe such that "nameDest" starting with "M"


I want to subset my dataset to only have data relating to the nameDest column values starting with 'M'

the data I am working on

This is the data I am working on. I want all the rows which have nameDest as Mxxxx

I tried to do it like this:

merchant = df.loc[df['nameDest'][0:] == 'M'].

It did not go well.


Solution

  • I figured it out. What I did is that I first created a list by parsing through that col and checking if any value starts with 'M' if it did then I appended it into the list. Then I used .isin() with .loc to subset the dataframe.

    merchant = []
    for i in df.nameDest:
        if i[0] == 'M':
            merchant.append(i)
    print(merchant[:30])
    
    merchants = df.loc[df['nameDest'].isin(merchant)]
    merchants.head()