Search code examples
pythonpandasdataframepandas-datareaderpandas-apply

How to use the apply function on a dataframe for retrieving a particular column?


I wrote the following code for downloading the dataset and apply EDA functions on the DataFrame

url = "https://query1.finance.yahoo.com/v7/finance/download/RELIANCE.BO?period1=1577110559&period2=1608732959&interval=1d&events=history&includeAdjustedClose=true"
r = requests.get(url)
open(stock+'.csv','wb').write(r.content)  
ril = pd.read_csv(r'RELIANCE.csv',date_parser='Date')
ril.head(10)

Here I want to retrieve the Close column via the apply column for practising with the df.apply() function

def close(stock):
    print(stock.iloc[:,6])
ril.apply(close)

But the code gave an IndexingError as

IndexingError                             Traceback (most recent call last)
<ipython-input-21-9fad7d447930> in <module>()
----> 1 asp.apply(close)

7 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _has_valid_tuple(self, key)
    698         for i, k in enumerate(key):
    699             if i >= self.ndim:
--> 700                 raise IndexingError("Too many indexers")
    701             try:
    702                 self._validate_key(k, i)

IndexingError: Too many indexers

Can it be done with the df.apply() function?


Solution

  • df = pd.read_csv(r'RELIANCE.csv',date_parser='Date')
    
    close1 = df['Close']                                 #standard way of assessing the column
    close2 = df.apply(lambda x: x.iloc[4] , axis=1)      #apply function row-wise: take 1
    close3 = df.apply(lambda x: x[4]      , axis=1)      # ... take 2
    close4 = df.apply(lambda x: x['Close'], axis=1)      # ... take 3
    
    print( np.allclose(close1, close2, equal_nan=True) ) # verify
    ...
    

    For references: pandas.DataFrame.iloc and pandas.DataFrame.apply

    Basically what happened in your case is that you iterated over your dataframe using pd.apply as well as by indexing df.iloc[:,...]. Note the axis=1 in order to apply the function row-wise