试图运行一个简短的程序,它给了我正确的结果,但最后它总是给我神​​秘的错误信息

时间:2017-03-17 00:05:16

标签: python macos python-2.7 pandas

以下代码从wiki中提取状态缩写,然后我附加到FMAC / HPI_AL,然后将其用作从数据集中提取值并将其存储在数据框中的键

import quandl
import pandas as pd

在这个软件中,我使用quandl和pandas模块

api_key = 'tVKGSq5NCWt6jr5p1m7Y'
def state_list():
    fiddy_states = pd.read_html('https://simple.wikipedia.org/wiki/List_of_U.S._states')
    return fiddy_states[0][0][1:]


def grab_initial_state_data():
    states = state_list()

    main_df = pd.DataFrame()

循环遍历州缩写并将它们附加到另一个字符串,这些字符串一起构成了从quandl中提取值的键。         对于州内的abbv:             query =" FMAC / HPI _" + str(abbv)             df = quandl.get(query,authtoken = api_key)             打印(查询)             if main_df.empty:                 main_df = df             其他:                 main_df = main_df.join(df)                 打印(main_df)             打印(main_df.head())     grab_initial_state_data()

输出的第一部分:

FMAC/HPI_AL
                Value
Date
1975-01-31  35.311045
1975-02-28  35.533128
1975-03-31  35.792522
1975-04-30  36.109691
1975-05-31  36.411170
FMAC/HPI_AK

在Mac上运行程序后,它会按预期生成正确的输出。 然而,系统通过打印以下神秘的错误消息来阻止我对代码进行任何操作。

Traceback (most recent call last):
  File "learning_picks.py", line 35, in <module>
    grab_initial_state_data()
  File "learning_picks.py", line 25, in grab_initial_state_data
    main_df = main_df.join(df)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 4555, in join
    rsuffix=rsuffix, sort=sort)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 4569, in _join_compat
    suffixes=(lsuffix, rsuffix), sort=sort)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/merge.py", line 62, in merge
    return op.get_result()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/merge.py", line 556, in get_result
    rdata.items, rsuf)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/internals.py", line 4699, in items_overlap_with_suffix
    to_rename)
ValueError: columns overlap but no suffix specified: Index([u'Value'], dtype='object')

非常感谢任何帮助或见解。

1 个答案:

答案 0 :(得分:0)

您的main_dfdf列中的列名重叠,但是在发生这种情况时您还没有指定要执行的操作。

要解决此错误,请执行以下操作:

main_df.join(df, lsuffix='_left', rsuffix='_right')

相反。