如何在数据框上使用 apply 函数来检索特定列?
How to use the apply function on a dataframe for retrieving a particular column?
我编写了以下代码来下载数据集并在 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()
函数来完成吗?
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.iloc and pandas.DataFrame.apply
基本上在您的案例中发生的事情是您使用 pd.apply
以及索引 df.iloc[:,...]
迭代了数据框。注意 axis=1
以便按行应用函数
我编写了以下代码来下载数据集并在 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()
函数来完成吗?
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.iloc and pandas.DataFrame.apply
基本上在您的案例中发生的事情是您使用 pd.apply
以及索引 df.iloc[:,...]
迭代了数据框。注意 axis=1
以便按行应用函数