这是什么类型的TypeError ?



有人明白这段代码的错误吗?似乎是最后一行代码有问题。

TypeError:/: 'str'和'str'不支持的操作数类型

BS = requests.get(f'https://financialmodelingprep.com/api/v3/balance-sheet-statement/{company}?apikey={demo}').json()
# get income statement as % of revenue for future predictions and forecast 5 next IS years
income_statement = pd.DataFrame.from_dict(IS[0], orient='index')
income_statement = income_statement[5:26]
income_statement.columns = ['current_year']
income_statement['as_%_of_revenue'] = income_statement /income_statement.iloc[0]
如果我改变代码
income_statement['as_%_of_revenue'] = income_statement /income_statement.iloc[0]

转换成int,然后给我

income_statement['as_%_of_revenue'] = int((income_statement) / int(income_statement)).iloc[0]
TypeError: int() argument must be a string, a bytes-like object or a number, not 'DataFrame'

尽管更多的研究可能已经解决了这个问题:

您正在尝试划分两个数字(我猜),但它们被格式化为str。试着把它们转换成可整除的东西,比如intfloat,简单的例子:

**income_statement['as_%_of_revenue'] = int(income_statement) / int(income_statement.iloc[0]**)

更新似乎你正试图将pandas Dataframe中的一行中的值除以该行中的另一个数字。如果你有Dataframedf = pd.DataFrame(data=([100, 200], [500, 500])):

0    1
0  100  200
1  500  500

您可以像这样添加一个包含第一列比第二列的百分比的列:
df['as_%'] = (df[0] / df[1])*100。结果是:

0    1   as_%
0  100  200   50.0
1  500  500  100.0

这一行缺少

net_income = IS[0]['netIncome']

这就是为什么"我想用最后一行实现什么"这个问题不清楚的原因。因此整个代码是:

net_income = IS[0]['netIncome']
BS = requests.get(f'https://financialmodelingprep.com/api/v3/balance-sheet-statement/{company}?apikey={demo}').json()

# get income statement as % of revenue for future predictions and forecast 5 next IS years
income_statement = pd.DataFrame.from_dict(IS[0], orient='index')
income_statement = income_statement[5:26]
income_statement.columns = ['current_year']
income_statement['as_%_of_revenue'] = income_statement /income_statement.iloc[0]

我想要实现的是将损益表除以收入(销售额)=收入/收入(本年)。这个结果将给我一个预测未来几年收入的百分比,这是一个乘数。

分割前的数据是这样的

问题是income_statement被定义为一个类,因为数据框

<class 'pandas.core.frame.DataFrame'>
current_year
period                                     FY
revenue                             495308000
costOfRevenue                        43839000
grossProfit                         451469000
grossProfitRatio                     0.911491
researchAndDevelopmentExpenses      101117000
generalAndAdministrativeExpenses    101439000
sellingAndMarketingExpenses         252820000
otherExpenses                               0
operatingExpenses                   455376000
costAndExpenses                     499215000
interestExpense                      38119000
depreciationAndAmortization          12101000
ebitda                              8.194e+06
ebitdaratio                         0.0165432
operatingIncome                    -3.907e+06
operatingIncomeRatio              -0.00788802
totalOtherIncomeExpensesNet          14382000
incomeBeforeTax                     -27645000
incomeBeforeTaxRatio               -0.0558138
incomeTaxExpense                            0

我刚刚接触python(3个月),所以我不确定所有简单的问题,我猜

谢谢你的帮助

更新我现在就这样做了(即使这会花费我更多的时间)

i declare also revenue

net_income = IS[0]['netIncome']
revenue = IS[0]["revenue"]

,像这样计算

income_statement['as_%_of_revenue'] = (net_income/revenue)*100

向前看,如果这引起任何进一步的问题,我猜

数据现在是这样的

current_year  as_%_of_revenue
period                                     FY        -4.920978
revenue                             495308000        -4.920978
costOfRevenue                        43839000        -4.920978
grossProfit                         451469000        -4.920978
grossProfitRatio                     0.911491        -4.920978
researchAndDevelopmentExpenses      101117000        -4.920978
generalAndAdministrativeExpenses    101439000        -4.920978
sellingAndMarketingExpenses         252820000        -4.920978
otherExpenses                               0        -4.920978
operatingExpenses                   455376000        -4.920978
costAndExpenses                     499215000        -4.920978
interestExpense                      38119000        -4.920978
depreciationAndAmortization          12101000        -4.920978
ebitda                              8.194e+06        -4.920978
ebitdaratio                         0.0165432        -4.920978
operatingIncome                    -3.907e+06        -4.920978
operatingIncomeRatio              -0.00788802        -4.920978
totalOtherIncomeExpensesNet          14382000        -4.920978
incomeBeforeTax                     -27645000        -4.920978
incomeBeforeTaxRatio               -0.0558138        -4.920978
incomeTaxExpense                            0        -4.920978
netIncome                           -24374000        -4.920978

很简单,你可以试试这个&;income_statement = income_statement[6:26]&;

最新更新