mercredi 27 décembre 2017

Drop line in pandas df when string type cell's right characters don't match condition

I am working on a dataframe containing demographic data for every single U.S state and county.

FIPS    State   Area_Name   CENSUS_2010_POP ESTIMATES_BASE_2010 ...
01000   AL  Alabama         4779736         4780131             ...    
01001   AL  Autauga County  54571           54571               ...      
01003   AL  Baldwin County  182265          182265              ...
01005   AL  Barbour County  27457           27457               ...

...     ... ...             ...             ...                 ...

I would like to drop all the lines regarding counties in order to keep only lines regarding U.S states (that's a lot of lines to drop indeed!). My idea was to focus on the FIPS column and to keep only FIPSs ending with '000', which are corresponding to states. After converting the FIPS into strings, I tried the following:

for k in df.index:
    if df.iloc[k,0][-3:] != '000':
        df=df.drop(df.index[k])

I am getting the following error: single positional indexer is out-of-bounds.

Aucun commentaire:

Enregistrer un commentaire