1

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.

8
  • 1
    df2 = df1.loc[countries] should works Commented Nov 18, 2017 at 18:42
  • 1
    If Country is a column, not the name of the index you need df1[df1['Country'].isin(countries)] Commented Nov 18, 2017 at 18:44
  • OMG - I knew it was simple. So simple that no one has probably asked it before. Commented Nov 18, 2017 at 18:44
  • Country is the index. I just tried @jezrael solution and it worked. Feel like a bit of a newb! (well I am for python) Commented Nov 18, 2017 at 18:45
  • @ayhan - csn you check if is dupe for this? Because now it is marked bad... Commented Nov 18, 2017 at 18:46

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.