[英]How can I find out which operands are supported before looping through pandas dataframe?
I am attempting to iterate over rows in a Series within a Pandas DataFrame. 我试图在Pandas DataFrame中迭代系列中的行。 I would like to take the value in each row of the column csv_df['Strike']
and plug it into variable K
, which gets called in function a
. 我想取csv_df['Strike']
列的每一行中的值并将其插入到变量K
,该变量在函数a
被调用。
Then, I want the output a1
and a2
to be put into their own columns within the DataFrame. 然后,我希望将输出a1
和a2
放入DataFrame中自己的列中。
I am receiving the error: TypeError: unsupported operand type(s) for *: 'int' and 'zip'
, and I figure that if I can find out which operands are supported, I could convert a1
and a2
to that. 我收到错误: TypeError: unsupported operand type(s) for *: 'int' and 'zip'
,我想如果我能找出支持哪些操作数,我可以将a1
和a2
转换为它。
Am I thinking about this correctly? 我正确地考虑了这个吗?
Note: S
is just a static number as the df is just one row, while K
has many rows. 注意: S
只是一个静态数字,因为df只是一行,而K
有很多行。
Code is below: 代码如下:
from scipy.stats import norm
from math import sqrt, exp, log, pi
import pandas as pd
pd.core.common.is_list_like = pd.api.types.is_list_like
import fix_yahoo_finance as yf
yf.pdr_override()
import numpy as np
import datetime
from pandas_datareader import data, wb
import matplotlib.pyplot as plt
#To get data:
start = datetime.datetime.today()
end = datetime.datetime.today()
df = data.get_data_yahoo('AAPL', start, end) #puts data into a pandas dataframe
csv_df = pd.read_csv('./AAPL_TEST.csv')
for row in csv_df.itertuples():
def a(S, K):
a1 = 100 * K
a2 = S
return a1
S = df['Adj Close'].items()
K = csv_df['strike'].items()
a1, a2 = a(S, K)
df['new'] = a1
df['new2'] = a2
It seems an alternate way of doing what you want would be to apply your method to each data frame separately, as in: 看起来你想做的事情的另一种方法是将你的方法分别应用于每个数据框,如:
df = data.get_data_yahoo('AAPL', start, end)
csv_df = pd.read_csv('./AAPL_TEST.csv')
df['new'] = csv_df['strike'].apply(lambda x: 100 * x)
df['new2'] = df['Adj Close']
Perhaps, applying the calculation directly to the Pandas Series (a column of your data frame) is a way to avoid defining a method that is used only once. 也许,将计算直接应用于Pandas系列(数据框的一列)是避免定义仅使用一次的方法的一种方法。 Plus, I wouldn't define a method within a loop as you have. 另外,我不会像你一样在循环中定义一个方法。 Cheers 干杯
ps. PS。 I believe you have forgotten to return both values in your method. 我相信你忘了在你的方法中返回两个值。
def a(S, K):
a1 = 100 * K
a2 = S
return (a1, a2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.