我想创建一个从API获取数据的函数,然后创建另一个函数来创建和清理相应的数据框以供使用。
第一组def如下所示,并且工作正常:
def get_data():
print('start download the 1st set')
confirm_details = requests.get('https://api.data.gov.hk/v2/filter?q=%7B%22resource%22%3A%22http%3A%2F%2Fwww.chp.gov.hk%2Ffiles%2Fmisc%2Fenhanced_sur_covid_19_eng.csv%22%2C%22section%22%3A1%2C%22format%22%3A%22json%22%7D').content
print('complete download the 1st set')
print('start download the 2nd set')
latest_situ = requests.get('https://api.data.gov.hk/v2/filter?q=%7B%22resource%22%3A%22http%3A%2F%2Fwww.chp.gov.hk%2Ffiles%2Fmisc%2Flatest_situation_of_reported_cases_covid_19_eng.csv%22%2C%22section%22%3A1%2C%22format%22%3A%22json%22%7D').content
print('complete download the 2nd set')
print('start download the final set')
residential = requests.get('https://api.data.gov.hk/v2/filter?q=%7B%22resource%22%3A%22http%3A%2F%2Fwww.chp.gov.hk%2Ffiles%2Fmisc%2Fbuilding_list_eng.csv%22%2C%22section%22%3A1%2C%22format%22%3A%22json%22%7D').content
print('complete download the final set')
get_data()
第二个定义如下,但它说给我抛出一个错误,即“ NameError:未定义名称'confirm_details':
def clean_confirm_df():
confirm_df = pd.read_json(io.StringIO(confirm_details.decode('utf-8')))
confirm_df.columns = confirm_df.columns.str.replace(" ", "_" )
confirm_df.columns = confirm_df.columns.str.replace('/', "_")
confirm_df.columns = confirm_df.columns.str.replace("*", "")
confirm_df.columns = confirm_df.columns.str.strip()
confirm_df['Report_date'] = pd.to_datetime(confirm_df['Report_date'], dayfirst=True)
confirm_df.rename(columns = {'Confirmed_probable': 'Confirmed'}, inplace = True)
confirm_df = confirm_df.drop(['Name_of_hospital_admitted', 'Date_of_onset'], axis = 1)
confirm_df['HK_Non-HK_resident'] = confirm_df['HK_Non-HK_resident'].str.upper()
confirm_df.head()
clean_confirm_df()
我看第一个def,我看到定义了“ confirm_details”。我试过了,分别创建df作品(confirm_df,latest_situ_df和Residential_df)的代码在单独运行时工作正常。
我正在自学python和pandas,感谢任何建议,我应该如何更改代码以使其正常工作。
谢谢。
根据注释-结构代码,以便您了解变量的范围。您已经假设一切都是全球性的,这将是一件非常糟糕的事情……
def get_data():
ret = {}
print('start download the 1st set')
ret["confirm_details"] = requests.get('https://api.data.gov.hk/v2/filter?q=%7B%22resource%22%3A%22http%3A%2F%2Fwww.chp.gov.hk%2Ffiles%2Fmisc%2Fenhanced_sur_covid_19_eng.csv%22%2C%22section%22%3A1%2C%22format%22%3A%22json%22%7D').content
print('complete download the 1st set')
print('start download the 2nd set')
ret["latest_situ"] = requests.get('https://api.data.gov.hk/v2/filter?q=%7B%22resource%22%3A%22http%3A%2F%2Fwww.chp.gov.hk%2Ffiles%2Fmisc%2Flatest_situation_of_reported_cases_covid_19_eng.csv%22%2C%22section%22%3A1%2C%22format%22%3A%22json%22%7D').content
print('complete download the 2nd set')
print('start download the final set')
ret["residential"] = requests.get('https://api.data.gov.hk/v2/filter?q=%7B%22resource%22%3A%22http%3A%2F%2Fwww.chp.gov.hk%2Ffiles%2Fmisc%2Fbuilding_list_eng.csv%22%2C%22section%22%3A1%2C%22format%22%3A%22json%22%7D').content
print('complete download the final set')
return ret
def clean_confirm_df(data):
confirm_df = pd.read_json(io.StringIO(data["confirm_details"].decode('utf-8')))
confirm_df.columns = confirm_df.columns.str.replace(" ", "_" )
confirm_df.columns = confirm_df.columns.str.replace('/', "_")
confirm_df.columns = confirm_df.columns.str.replace("*", "")
confirm_df.columns = confirm_df.columns.str.strip()
confirm_df['Report_date'] = pd.to_datetime(confirm_df['Report_date'], dayfirst=True)
confirm_df.rename(columns = {'Confirmed_probable': 'Confirmed'}, inplace = True)
confirm_df = confirm_df.drop(['Name_of_hospital_admitted', 'Date_of_onset'], axis = 1)
confirm_df['HK_Non-HK_resident'] = confirm_df['HK_Non-HK_resident'].str.upper()
return confirm_df
mydata = get_data()
df = clean_confirm_df(mydata)
print(df.head().to_markdown())
start download the 1st set
complete download the 1st set
start download the 2nd set
complete download the 2nd set
start download the final set
complete download the final set
| | Case_no. | Report_date | Gender | Age | Hospitalised_Discharged_Deceased | HK_Non-HK_resident | Case_classification | Confirmed |
|---:|-----------:|:--------------------|:---------|------:|:-----------------------------------|:---------------------|:----------------------|:------------|
| 0 | 1 | 2020-01-23 00:00:00 | M | 39 | Discharged | NON-HK RESIDENT | Imported case | Confirmed |
| 1 | 2 | 2020-01-23 00:00:00 | M | 56 | Discharged | HK RESIDENT | Imported case | Confirmed |
| 2 | 3 | 2020-01-24 00:00:00 | F | 62 | Discharged | NON-HK RESIDENT | Imported case | Confirmed |
| 3 | 4 | 2020-01-24 00:00:00 | F | 62 | Discharged | NON-HK RESIDENT | Imported case | Confirmed |
| 4 | 5 | 2020-01-24 00:00:00 | M | 63 | Discharged | NON-HK RESIDENT | Imported case | Confirmed |
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句