I am using Quandl to download daily NAV prices for a specific set of Mutual Fund schemes. However it returns a data object instead of returning the specific value
import quandl
import pandas as pd
quandl.ApiConfig.api_key = <Quandl Key>
list2 = [102505, 129221, 102142, 103197, 100614, 100474, 102913, 102921]
def get_nav(mf_code):
df_main=pd.DataFrame()
code=str(mf_code)
df_main=quandl.get("AMFI/"+code,start_date='2019-04-05',end_date='2019-04- 05')
return (df_main['Net Asset Value'])
for each in list2:
mf_code=each
nav = get_nav(mf_code)
print (nav)
Date
2019-04-05 29.8916
Name: Net Asset Value, dtype: float64
Date
2019-04-05 19.354
Name: Net Asset Value, dtype: float64
whereas,
I am looking to extract only the values i.e. 29.8916, 19.354, etc
Updated code:
def get_nav(mf_code):
nav1=[]
df_main=pd.DataFrame()
code=str(mf_code)
# try:
df_main=quandl.get("AMFI/"+code,start_date='2019-04-05',end_date='2019-04-05')
nav_value=df_main['Net Asset Value']
if not nav_value.empty:
nav1=nav_value[0]
print(nav1)
# print(df_main.head())
# except IndexError:
# nav_value=0
return (nav1)
#Use merged sheet for work
df_port=pd.read_excel(fp_out)
df_port['Current Price']=df_port['Scheme_Code'].apply(lambda x:get_nav(x))
print(df_port['Current Price'].head())
df_port.to_excel(fp_out2)
By default, quandl Time-series API returns you a dataframe with date as index, even if there is only one row.
If you only need the value of first row, you can use iloc
:
if not nav.empty:
print (nav.iloc[0])
or just plain integer indexing:
if not nav.empty:
print (nav[0])