【发布时间】:2018-04-12 17:09:25
【问题描述】:
我正在尝试连接每个股票代码的三个单独的数据帧(bs_df、income_df、cash_df),然后将它们附加到数据帧df中。它在第一次通过 for 循环时成功附加了数据帧,但是当它尝试将新的数据帧附加到原始数据帧时,它失败了,并抛出了 断言错误。我认为这是因为数据框具有不同的列(reference),但我无法找到绕过此问题的方法,并且仍然创建一个包含所有列名的数据框并在数据框没有值的情况下填写 NaN 值另一个数据框具有的特定列。
这是我正在运行的代码:
from yahoofinancials import YahooFinancials
import pandas as pd
tickers = ["PIH","AAPL","AMZN"]
df = pd.DataFrame()
for ticker in tickers:
print (ticker)
# Updates every loop
ticker_yahoo_financials = YahooFinancials(ticker)
# Get financial info
bs_hist = ticker_yahoo_financials.get_financial_stmts('annual', 'balance')
income_hist = ticker_yahoo_financials.get_financial_stmts('annual', 'income')
cash_hist = ticker_yahoo_financials.get_financial_stmts('annual', 'cash')
# Create dataframes and comebine them
bs_df = pd.DataFrame(list(bs_hist['balanceSheetHistory'][ticker][0].values()))
income_df = pd.DataFrame(list(income_hist['incomeStatementHistory'][ticker][0].values()))
cash_df = pd.DataFrame(list(cash_hist['cashflowStatementHistory'][ticker][0].values()))
comb = pd.concat([bs_df, income_df, cash_df], axis=1)
df = df.append(comb)
这是错误信息:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-23-f476ca9fbb70> in <module>()
19 cash_df = pd.DataFrame(list(cash_hist['cashflowStatementHistory'][ticker][0].values()))
20 comb = pd.concat([bs_df, income_df, cash_df], axis=1)
---> 21 df = df.append(comb)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in append(self, other, ignore_index, verify_integrity)
5192 to_concat = [self, other]
5193 return concat(to_concat, ignore_index=ignore_index,
-> 5194 verify_integrity=verify_integrity)
5195
5196 def join(self, other, on=None, how='left', lsuffix='', rsuffix='',
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
211 verify_integrity=verify_integrity,
212 copy=copy)
--> 213 return op.get_result()
214
215
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\reshape\concat.py in get_result(self)
406 new_data = concatenate_block_managers(
407 mgrs_indexers, self.new_axes, concat_axis=self.axis,
--> 408 copy=self.copy)
409 if not self.copy:
410 new_data._consolidate_inplace()
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in concatenate_block_managers(mgrs_indexers, axes, concat_axis, copy)
5205 blocks.append(b)
5206
-> 5207 return BlockManager(blocks, axes)
5208
5209
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in __init__(self, blocks, axes, do_integrity_check, fastpath)
3031
3032 if do_integrity_check:
-> 3033 self._verify_integrity()
3034
3035 self._consolidate_check()
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals.py in _verify_integrity(self)
3247 'block items\n# manager items: {0}, # '
3248 'tot_items: {1}'.format(
-> 3249 len(self.items), tot_items))
3250
3251 def apply(self, f, axes=None, filter=None, do_integrity_check=False,
AssertionError: Number of manager items must equal union of block items
# manager items: 67, # tot_items: 68
【问题讨论】:
标签: python pandas append concat