2

Simple question. I have a dataframe that looks like this (call if df1):

Country 2016 2017 2018
USA 1 2 3 
CAN 4 5 6
MEX 7 5 6
ARG 9 3 5 
GBR 5 8 2

I have a variable called countries which looks like this: countries=['USA','MEX','GBR']. How can I make a new dataframe (call it df2) which selects only the rows mentioned in countries?

I can select rows manually, or several of them if they are in a block (say USA to MEX), but I don't know how to do it if the order is random. A loop could do it, (see below) but that only saves the last country in the loop!

for country in countries:
    df2 = df1.loc[country:country,:].copy()

PS. I'm sure this is a duplicate! But I've spent a good hour searching for the answer with no success.

Agrippa
  • 475
  • 2
  • 5
  • 13

0 Answers0