My code started getting cluttered with functions so I wanted to make a library(or module I guess) of all my functions stored in it. Most functions that work in my code give the following error when importing them from a module:
takes 1 positional argument but 2 were given
I created a file called mylib.py.
The first function I placed in it, and of many that didn't work was this one:
def load(hist_indicator, data):
mask1 = data['IndicatorName'].str.contains(hist_indicator)
dfdata = data[mask1]
filter = dfdata['Year'] > 1995
df = dfdata[filter]
return df
All it does is it takes a pandas DataFrame and filters out data I need. The function works if it is in the code itself. I used it like this:
ind_internet = 'Internet users \(per 1'
internet = load(ind_internet, data)
However, when I tried using it from mylib.py I got the aforementioned error. Tried using it like this:
import mylib
ind_internet = 'Internet users \(per 1'
internet = mylib.load(ind_internet, data)
I hope you can let me know what I'm doing wrong.
Edit:
To add to the question, here's the code with a little example:
import pandas as pd
def load(hist_indicator, data):
mask1 = data['IndicatorName'].str.contains(hist_indicator)
dfdata = data[mask1]
filter = dfdata['Year'] > 1995
df = dfdata[filter]
return df
dataf = {'CountryName': ['France', 'Germany'],
'CountryCode': ['FRA', 'GER'],
'IndicatorName': ['Internet users (per 100)', 'GDP per capita (current US$)'],
'IndicatorCode' : ['MS.MIL.XPRT.KD','SP.POP.DPND.YG'],
'Year' : [2000,1983],
'Value' : [15.3,4322.27]
}
data = pd.DataFrame (dataf, columns = ['CountryName','CountryCode','IndicatorName','IndicatorCode','Year','Value'])
ind_internet = 'Internet users \(per 1'
internet = load(ind_internet, data)
internet
Also, an edit: I didn't actually type import mylib.py in the code, only here by mistake.