Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,754,532 members, 6,543,219 topics. Date: Monday, 18 October 2021 at 06:13 AM

I'm Getting An Error While I Run This Python Codes - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / I'm Getting An Error While I Run This Python Codes (1020 Views)

Help With This Python Programming Assignment Please / How Can Someone Convert His Python Codes To Apk / Python Masters, Kindly Help Me Rectify This Python Problem. (2) (3) (4)

(1) (Reply) (Go Down)

I'm Getting An Error While I Run This Python Codes by ibromodzi: 8:11am On Dec 07, 2019
Hello great programmers, I'm new to pandas so I just downloaded this data set from Kaggle.com to practice with. However the program throws an error saying Object DataFrame is not callable. The dataset can be found here.
Here is the code

import pandas as pd
import numpy as np
import researchpy as rp
import scipy.stats
import matplotlib.pyplot as plt

un = pd.read_csv(r"C:\Users\ibrom\Documents\SAMPLE DATASET\Military Expenditure.csv"wink
#To know the country with highest spending
#first create a new column with total spending over the years

un['Total Spending'] = un.iloc[:, 5:64].sum(axis=1)

#sorting values to know the country with the highest and least spending
un.sort_values('Total Spending', ascending = False)

#filtering based on Type of region
man = rp.summary_cat(un['Type'])
man.reset_index()

#grouyping the countries into continents and assessing their military spending over the year
def continent(country):
if country in ('Algeria','Angola','Benin','Botswana','Burkina Faso','Burundi',
'Cabo Verde','Cameroon',
'Central African Republic','Chad','Comoros',
'Congo, Democratic Republic of the','Congo, Republic of the','Cote d\'Ivoire','Djibouti','Egypt',
'Equatorial Guinea','Eritrea','Eswatini',' Ethiopia', 'Gabon','Gambia','Ghana',
'Guinea',"Guinea-Bissau", 'Kenya', 'Lesotho', 'Liberia', 'Libya', 'Madagascar', 'Malawi', 'Mali',
'Mauritania', 'Mauritius', 'Morocco', 'Mozambique', 'Namibia', 'Niger', 'Nigeria', 'Rwanda',
'Sao Tome and Principe' 'Senegal', 'Seychelles', 'Sierra Leone', 'Somalia', 'South Africa', 'South Sudan',
'Sudan', 'Tanzania', 'Togo', 'Tunisia', 'Uganda', 'Zambia', 'Zimbabwe'):
return "Africa"
elif country in ('Afghanistan', 'Armenia', 'Azerbaijan', 'Bahrain', 'Bangladesh', 'Bhutan', 'Brunei'
'Cambodia', 'China', 'Cyprus', 'Georgia' 'India' 'Indonesia' 'Iran' 'Iraq', 'Israel' 'Japan Jordan',
'Kazakhstan', 'Kuwait', 'Kyrgyzstan', 'Laos','Lebanon', 'Malaysia', 'Maldives', 'Mongolia',
'Myanmar (formerly Burma)', 'Nepal','North Korea', 'Oman', 'Pakistan', 'Palestine', 'Philippines',
'Qatar', 'Russia', 'Saudi Arabia', 'Singapore', 'South Korea', 'Sri Lanka', 'Syria', 'Taiwan',
'Tajikistan', 'Thailand', 'Timor-Leste','Turkey','Turkmenistan', 'United Arab Emirates (UAE)',
'Uzbekistan', 'Vietnam', 'Yemen, Rep'):
return "Asia"
elif country in ('Albania', 'Andorra', 'Armenia', 'Austria', 'Azerbaijan', 'Belarus', 'Belgium', 'Bosnia and Herzegovina',
'Bulgaria', 'Croatia', 'Cyprus', 'Czechia', 'Denmark', 'Estonia', 'Finland', 'France', 'Georgia',
'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland', 'Italy', 'Kazakhstan', 'Kosovo', 'Latvia',
'Liechtenstein', 'Lithuania', 'Luxembourg', 'Malta', 'Moldova', 'Monaco', 'Montenegro',
'Netherlands', 'North Macedonia', 'Norway', 'Poland', 'Portugal','Romania' 'Russia' 'San Marino' 'Serbia',
'Slovakia', 'Slovenia', 'Spain', 'Sweden', 'Switzerland', 'Turkey', 'Ukraine', 'United Kingdom',
'Vatican City'):
return "Europe"
elif country in ('Antigua and Barbuda', 'Bahamas', 'Barbados', 'Belize Canada', 'Costa Rica', 'Cuba',
'Dominica Dominican Republic', 'El Salvador', 'Grenada', 'Guatemala', 'Haiti', 'Honduras', 'Jamaica',
'Mexico', 'Nicaragua', 'Panama', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent and the Grenadines',
'Trinidad and Tobago', 'United States'):
return "North America"
elif country in ('Argentina', 'Bolivia', 'Brazil', 'Chile', 'Colombia', 'Ecuador', 'Guyana', 'Paraguay', 'Peru',
'Suriname', 'Uruguay', 'United States', 'Venezuela'):
return "South America"
elif country in ('Australia', 'Fiji', 'Kiribati', 'Marshall', 'Islands', 'Micronesia', 'Nauru', 'New Zealand',
'Palau', 'Papua New Guinea', 'Samoa', 'Solomon Islands', 'Tonga', 'Tuvalu', 'Vanuatu'):
return "Australia / Oceania"

# Creating the new column for continent
un['Continent'] = un['Name'].apply(continent)

# Create two new dataframes which are subsets of the original data
Europe = un(un['Continent'] == 'Europe')
Europe.reset_index(inplace = True)
NorthAmericaa = un(un['Continent'] == 'North America')
Re: I'm Getting An Error While I Run This Python Codes by stanliwise(m): 9:58am On Dec 07, 2019
We are not magicians, tell us the error Message you saw
Re: I'm Getting An Error While I Run This Python Codes by ibromodzi: 11:03am On Dec 07, 2019
stanliwise:
We are not magicians, tell us the error Message you saw

I have stated it in my intro, the error is: DataFrame object is not callable when I tried to execute the last four lines of the code i.e

# Create two new dataframes which are subsets of the original data
Europe = un(un['Continent'] == 'Europe')
Europe.reset_index(inplace = True)
NorthAmericaa = un(un['Continent'] == 'North America')
Re: I'm Getting An Error While I Run This Python Codes by ibromodzi: 2:27pm On Dec 07, 2019
ibromodzi:
Hello great programmers, I'm new to pandas so I just downloaded this data set from Kaggle.com to practice with. However the program throws an error saying Object DataFrame is not callable. The dataset can be found here.
Here is the code

import pandas as pd
import numpy as np
import researchpy as rp
import scipy.stats
import matplotlib.pyplot as plt

un = pd.read_csv(r"C:\Users\ibrom\Documents\SAMPLE DATASET\Military Expenditure.csv"wink
#To know the country with highest spending
#first create a new column with total spending over the years

un['Total Spending'] = un.iloc[:, 5:64].sum(axis=1)

#sorting values to know the country with the highest and least spending
un.sort_values('Total Spending', ascending = False)

#filtering based on Type of region
man = rp.summary_cat(un['Type'])
man.reset_index()

#grouyping the countries into continents and assessing their military spending over the year
def continent(country):
if country in ('Algeria','Angola','Benin','Botswana','Burkina Faso','Burundi',
'Cabo Verde','Cameroon',
'Central African Republic','Chad','Comoros',
'Congo, Democratic Republic of the','Congo, Republic of the','Cote d\'Ivoire','Djibouti','Egypt',
'Equatorial Guinea','Eritrea','Eswatini',' Ethiopia', 'Gabon','Gambia','Ghana',
'Guinea',"Guinea-Bissau", 'Kenya', 'Lesotho', 'Liberia', 'Libya', 'Madagascar', 'Malawi', 'Mali',
'Mauritania', 'Mauritius', 'Morocco', 'Mozambique', 'Namibia', 'Niger', 'Nigeria', 'Rwanda',
'Sao Tome and Principe' 'Senegal', 'Seychelles', 'Sierra Leone', 'Somalia', 'South Africa', 'South Sudan',
'Sudan', 'Tanzania', 'Togo', 'Tunisia', 'Uganda', 'Zambia', 'Zimbabwe'):
return "Africa"
elif country in ('Afghanistan', 'Armenia', 'Azerbaijan', 'Bahrain', 'Bangladesh', 'Bhutan', 'Brunei'
'Cambodia', 'China', 'Cyprus', 'Georgia' 'India' 'Indonesia' 'Iran' 'Iraq', 'Israel' 'Japan Jordan',
'Kazakhstan', 'Kuwait', 'Kyrgyzstan', 'Laos','Lebanon', 'Malaysia', 'Maldives', 'Mongolia',
'Myanmar (formerly Burma)', 'Nepal','North Korea', 'Oman', 'Pakistan', 'Palestine', 'Philippines',
'Qatar', 'Russia', 'Saudi Arabia', 'Singapore', 'South Korea', 'Sri Lanka', 'Syria', 'Taiwan',
'Tajikistan', 'Thailand', 'Timor-Leste','Turkey','Turkmenistan', 'United Arab Emirates (UAE)',
'Uzbekistan', 'Vietnam', 'Yemen, Rep'):
return "Asia"
elif country in ('Albania', 'Andorra', 'Armenia', 'Austria', 'Azerbaijan', 'Belarus', 'Belgium', 'Bosnia and Herzegovina',
'Bulgaria', 'Croatia', 'Cyprus', 'Czechia', 'Denmark', 'Estonia', 'Finland', 'France', 'Georgia',
'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland', 'Italy', 'Kazakhstan', 'Kosovo', 'Latvia',
'Liechtenstein', 'Lithuania', 'Luxembourg', 'Malta', 'Moldova', 'Monaco', 'Montenegro',
'Netherlands', 'North Macedonia', 'Norway', 'Poland', 'Portugal','Romania' 'Russia' 'San Marino' 'Serbia',
'Slovakia', 'Slovenia', 'Spain', 'Sweden', 'Switzerland', 'Turkey', 'Ukraine', 'United Kingdom',
'Vatican City'):
return "Europe"
elif country in ('Antigua and Barbuda', 'Bahamas', 'Barbados', 'Belize Canada', 'Costa Rica', 'Cuba',
'Dominica Dominican Republic', 'El Salvador', 'Grenada', 'Guatemala', 'Haiti', 'Honduras', 'Jamaica',
'Mexico', 'Nicaragua', 'Panama', 'Saint Kitts and Nevis', 'Saint Lucia', 'Saint Vincent and the Grenadines',
'Trinidad and Tobago', 'United States'):
return "North America"
elif country in ('Argentina', 'Bolivia', 'Brazil', 'Chile', 'Colombia', 'Ecuador', 'Guyana', 'Paraguay', 'Peru',
'Suriname', 'Uruguay', 'United States', 'Venezuela'):
return "South America"
elif country in ('Australia', 'Fiji', 'Kiribati', 'Marshall', 'Islands', 'Micronesia', 'Nauru', 'New Zealand',
'Palau', 'Papua New Guinea', 'Samoa', 'Solomon Islands', 'Tonga', 'Tuvalu', 'Vanuatu'):
return "Australia / Oceania"

# Creating the new column for continent
un['Continent'] = un['Name'].apply(continent)

# Create two new dataframes which are subsets of the original data
Europe = un(un['Continent'] == 'Europe')
Europe.reset_index(inplace = True)
NorthAmericaa = un(un['Continent'] == 'North America')

Problem solved. Thanks!

(1) (Reply)

Programming of GSTECH OST To PST Software / Java Programmers, Check This Simple Application And Evaluate It. / Algorithm Behind Voice Compression And Transfer Over Low-bandwidth Networks

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2021 Oluwaseun Osewa. All rights reserved. See How To Advertise. 79
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.