【问题标题】:How to use the apply function on a dataframe for retrieving a particular column?如何在数据帧上使用 apply 函数来检索特定列?
【发布时间】:2021-04-05 09:02:06
【问题描述】:

我编写了以下代码来下载数据集并在 DataFrame 上应用 EDA 函数

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)

这里我想通过apply列检索Close列来练习df.apply()函数

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

但是代码给出了IndexingError作为

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

可以用df.apply()函数完成吗?

【问题讨论】:

  • 发布整个回溯
  • 我已经更新了我的问题

标签: python pandas dataframe pandas-datareader pandas-apply


【解决方案1】:
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
...

供参考:pandas.DataFrame.ilocpandas.DataFrame.apply

在您的案例中发生的基本上是您使用pd.apply 以及通过索引df.iloc[:,...] 来迭代您的数据框。请注意axis=1,以便按行应用函数

【讨论】:

  • 迭代是按行还是按元素作为二维数组进行?
  • 您的问题到底是什么? pandas 文档将选项 axis=1 描述为:“将函数应用于每一行”。除此之外,DataFrame 严格来说不是二维数组!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
相关资源
最近更新 更多