如何通过行标签跳过数据框行

时间:2019-08-25 15:33:40

标签: python pandas dataframe

我试图跳过对一个特定的DataFrame行进行求和,因为但是这样做的时候,我得到了ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or 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
-----------------------------------------------------------------

因此,我尝试使用reportDate跳过df.loc['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

我该如何解决?

1 个答案:

答案 0 :(得分:1)

一种解决方法

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

按其工作方式求和: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)

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