itsrobwhite
itsrobwhite

Reputation: 1

How do I drop or select specific rows?

I have the following data and would like to drop any rows where the LSOA code doesn't start with 'E01' or alternatively create a new data frame with only the rows that start with 'E01'. Can anyone help with this?

    LSOA code   0
0   E01000001   359
1   E01000002   336
2   E01000003   68
3   E01000005   748
4   E01000006   110
... ... ...
6228    W01001867   1
6229    W01001870   3
6230    W01001913   1
6231    W01001938   1
6232    W01001941   1

Upvotes: 0

Views: 31

Answers (1)

mabergerx
mabergerx

Reputation: 1213

Following the Pandas documentation:

df_new = df[df['LSOA code'].str.startswith('E01')]

This returns a new dataframe df_new which only contains rows that start with E01.

If you want to have a df that does not have those rows, do the opposite:

df_new = df[~df['LSOA code'].str.startswith('E01')]

Upvotes: 1

Related Questions