I'm trying to extract data for a list of counties using a for loop. However, it's not working at all. DATA
only contains the 21 rows for Orange County after the loop.
import pandas as pd
df = pd.read_csv('https://query.data.world/s/7ifvvpyy23sa2fciuvcygehakxlnnv')
COUNTIES = ['Orange','Los Angeles','Imperial']
DATA = df.loc[df['county']==COUNTIES[0]]
for cnty in COUNTIES[1:]:
DATA.append(df.loc[df['county']==cnty], ignore_index=True)
print(DATA.shape)
print(DATA)
Can somebody tell me what went wrong? I tried
df.loc[df['county']=='Orange'].append(df.loc[df['county']=='Los Angeles'])
and it worked fine. But
DATA.append(df.loc[df['county']=='Los Angeles'])
simply did nothing. What happened when I assigned the selected rows to the dataframe DATA
?