0

enter image description here

I want to filter out country belonging to the tuple need and display every data in the records for countries after filter...

import pandas as pd
import os
import xlwt
import xlrd


file = 'ida.xlsx'
data = pd.read_excel(file)
need =("Algeria", "Angola", "Argentina", "Bangladesh", "Belize", "Benin", "Bhutan", "Bolivia", "Botswana", "Brazil", "Burkina Faso", "Burundi", 
"Cameroon", "Cape Verde", "Central African Rep.", "Chad", "Chile", "China", "Colombia", "Comoros", "Congo, Dem. Rep.", "Congo, Republic", 
"Costa Rica", "Cote d'Ivoire", "Cuba", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Ethiopia", "Fiji", "Gabon", "Gambia", "Ghana", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", 
"India", "Indonesia", "Iran", "Jamaica", "Jordan", "Kenya", "Kiribati", "Kyrgyzstan", "Laos", "Lebanon", "Lesotho", "Madagascar", "Malawi", 
"Malaysia", "Maldives", "Mali", "Marshall Island", "Mauritania", "Mauritius", "Mexico", "Mongolia", "Morocco", "Mozambique", "Myanmar", 
"Namibia", "Nepal", "Nicaragua", "Niger", "Pakistan", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Rwanda", "Samoa", 
"Sao Tome and Principe", "Senegal", "Seychelles", "Sierra Leone", "Solomon Islands", "South Africa", "Sri Lanka", "St Vincent and the Grenadines", "St.Kitts", "St.Lucia", "Sudan", "Suriname", "Swaziland", "Tanzania", "Thailand", "Togo", "Tonga", "Tunisia", "Turkey", 
"Uganda", "Uruguay", "Vanuatu", "Venezuela", "Viet Nam", "Zambia", "Zimbabwe")
for i in data.index:
    filt = data[data['Recipient'][i] == need]
    print(filt)

i get .

============== RESTART: C:/Users/robinson/Desktop/next set/done.py ============= Empty DataFrame Columns: [Recipient, Unnamed: 1, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018] Index: []

instead of data of each country and year...

3
  • Please provide sample input and expected output. Commented May 4, 2020 at 21:45
  • What did you get when you run print(data)? Maybe you have to clear all unwanted content (first excel lines) of your dataframe. Have you done that? Commented May 4, 2020 at 22:12
  • yes i've run print(data) and this is what i got Empty DataFrame Columns: [Recipient, Unnamed: 1, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018] Index: [] >>> Commented May 4, 2020 at 22:16

1 Answer 1

2

Forget the for loop and just use:

# for i in data.index: # <-- remove this
filt = data[data['Recipient'].isin(need)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.