gpt4 book ai didi

python - 如何逐行跳过 DataFrame 标签

转载 作者:行者123 更新时间:2023-12-01 07:22:09 24 4
gpt4 key购买 nike

我试图跳过对一个特定的 DataFrame 行进行求和,因为但是当我这样做时,我得到了 ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

我知道这是一个常见错误,但我已经阅读了很多文章/帖子,但仍然无法弄清楚。

原代码为:

import os
from iexfinance.stocks import Stock
import pandas as pd

# Set IEX Finance API Token
os.environ['IEX_API_VERSION'] = 'v1'
os.environ['IEX_TOKEN'] = 'token'

df = pd.read_csv("input.csv")

for index, row in df.iterrows():
symbol = (row["Symbol"])
company = Stock(symbol, output_format='pandas')
df_cash_flow = company.get_cash_flow(period="quarter", last='4')
df_cash_flow['TTM'] = df_cash_flow.sum(axis = 1)
print(df_cash_flow)

输出为:


| 6/30/19 | 4/9/19 | 1/5/19 | 10/5/18 | TTM
-----------------------------------------------------------------
capitalExpenditures | 123 | 456 | 789 | 101 | 1469
-----------------------------------------------------------------
cashChange | 101 | 633 | 453 | 902 | 2089
-----------------------------------------------------------------
............
-----------------------------------------------------------------
reportDate | 6/30/19 | 4/9/19 | 1/5/19 | 10/5/18 | 2019-06-302019-04-09...
-----------------------------------------------------------------
depreciation | 764 | 122 | 423 | 199 | 1508
-----------------------------------------------------------------

但我只想输出是:

                    | 6/30/19 | 4/9/19 | 1/5/19 | 10/5/18 | TTM
-----------------------------------------------------------------
capitalExpenditures | 123 | 456 | 789 | 101 | 1469
-----------------------------------------------------------------
cashChange | 101 | 633 | 453 | 902 | 2089
-----------------------------------------------------------------
............
-----------------------------------------------------------------
reportDate | 6/30/19 | 4/9/19 | 1/5/19 | 10/5/18 |
-----------------------------------------------------------------
depreciation | 764 | 122 | 423 | 199 | 1508
-----------------------------------------------------------------

因此,我尝试使用 df.loc['reportDate'] 跳过 reportDate 行标签:

df_cash_flow = company.get_cash_flow(period="quarter", last='4')
if df_cash_flow.loc['reportDate']:
pass
else:
df_cash_flow['TTM'] = df_cash_flow.sum(axis = 1)

但是会返回ValueError

我该如何解决这个问题?

最佳答案

解决方法

如果问题只是单个单元格,则可以接受诸如仅丢弃不需要的结果之类的解决方法。

按其工作方式进行求和:df_cash_flow['TTM'] = df_cash_flow.sum(axis = 1)
然后执行:

df_cash_flow.loc['reportDate', 'TTM'] = ''

这样,您就可以将 'reportDate' 行和 'TTM' 列的单元格中的值替换为空字符串。

正确的解决方案

正确的解决方案是在求和之前仅选择所需的行:

df_cash_flow['TTM'] = df_cash_flow.loc[df_cash_flow.index.drop('reportDate')].sum(axis=1)

通过从 index 中删除 'reportDate',仅对其他行执行求和。您在 'reportDate' 行和 'TTM' 列的单元格中得到 NaN
如果需要,可以轻松扩展此解决方案以排除其他行。只需删除您不想求和的所有索引标签,将它们放入列表中即可:df_cash_flow.index.drop(['reportDate', 'otherlabel1', 'otherlabel2'])

关于python - 如何逐行跳过 DataFrame 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57647391/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com