1
我試圖從一個pandas dataframe
加入到另一個列中,使用其日期作爲索引。但是,我的代碼生成TypeError
。請有人可以解釋爲什麼這個錯誤正在產生,我能做些什麼來解決它?嘗試按索引加入熊貓數據框時出現TypeError
在此先感謝。
"""Balsjo THg, MeHg, TOC | Q and flux data 2005-2010"""
path2 = 'C:\\Users\\Google Drive\\SLU\\Thesis\\Data\\BalsjSite4.xls'
df2 = pd.io.excel.read_excel(path2, 'THg', header=0, parse_dates=[1], dayfirst=True, index_col='Date')
df3 = pd.io.excel.read_excel(path2, 'MeHg', header=0, parse_dates=[1], dayfirst=True, index_col='Date')
df4 = pd.io.excel.read_excel(path2, 'TOC', header=0, parse_dates=[1], dayfirst=True, index_col='Date')
print list(df2.columns.values)
print list(df3.columns.values)
print list(df4.columns.values)
cln_df2 = df2[['Q (mm/d)', 'THg (ng/L)', 'Flux']]
cln_df22 = cln_df2.append[df3[['MeHg (ng/L)']]]
print cln_df22.head()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-4f468a92d5ce> in <module>()
9 print list(df4.columns.values)
10 cln_df2 = df2[['Q (mm/d)', 'THg (ng/L)', 'Flux']]
---> 11 cln_df22 = cln_df2.append[df3[['MeHg (ng/L)']]]
12 print cln_df22.head()
TypeError: 'instancemethod' object has no attribute '__getitem__'
[u'Q (mm/d)', u'THg (ng/L)', u'RIM THg', u'Flux', u'Rim Flux', u'Error', 'Unnamed: 7', 'Unnamed: 8', 'Unnamed: 9', 'Unnamed: 10', 'Unnamed: 11', 'Unnamed: 12', 'Unnamed: 13', 'Unnamed: 14', 'Unnamed: 15', u'z', u'ae(bz)', u'c0e(fz)']
[u'Q (mm/d)', u'MeHg (ng/L)', u'RIM MeHg', u'Flux', u'Rim Flux', u'Error', 'Unnamed: 7', 'Unnamed: 8', 'Unnamed: 9', 'Unnamed: 10', 'Unnamed: 11', 'Unnamed: 12', 'Unnamed: 13', 'Unnamed: 14', 'Unnamed: 15', u'z', u'ae(bz)', u'c0e(fz)']
[u'Q (mm/d)', u'TOC (mg/l)', u'RIM TOC', u'Flux', u'Rim Flux', u'Error', 'Unnamed: 7', 'Unnamed: 8', 'Unnamed: 9', 'Unnamed: 10', 'Unnamed: 11', 'Unnamed: 12', 'Unnamed: 13', 'Unnamed: 14', 'Unnamed: 15', u'z', u'ae(bz)', u'c0e(fz)']
我認爲你要做到這一點'cln_df22 = cln_df2.append(DF3 [ '甲基汞(納克/升)']) ',你試圖從append屬性的數據框中訪問不存在的項目 – EdChum
@EdChum好的,謝謝你的解決方案和解釋。 – Bprodz