我有两个数据框:
df_energy.info()
<class 'pandas.core.frame.DataFrame'> Int64Index: 34673 entries, 1 to 43228 Data columns (total 6 columns): TIMESTAMP 34673 non-null datetime64[ns] P_ACT_KW 34673 non-null float64 PERIODE_TARIF 34673 non-null object P_SOUSCR 34673 non-null float64 SITE 34673 non-null object TARIF 34673 non-null object dtypes: datetime64[ns](1), float64(2), object(3) memory usage: 1.9+ MB
和df1:
df1.info()
<class 'pandas.core.frame.DataFrame'> RangeIndex: 38840 entries, 0 to 38839 Data columns (total 7 columns): TIMESTAMP 38840 non-null datetime64[ns] ACT_TIME_AERATEUR_1_F1 38696 non-null float64 ACT_TIME_AERATEUR_1_F3 38697 non-null float64 ACT_TIME_AERATEUR_1_F5 38695 non-null float64 ACT_TIME_AERATEUR_1_F6 38695 non-null float64 ACT_TIME_AERATEUR_1_F7 38693 non-null float64 ACT_TIME_AERATEUR_1_F8 38696 non-null float64 dtypes: datetime64[ns](1), float64(6) memory usage: 2.1 MB
我尝试根据TIMESTAMP列合并这两个数据框:
merged_df_energy = pd.merge(df_energy.set_index('TIMESTAMP'),
df1,
right_index=True,
left_index =True)
但是我得到这个错误:
TypeError Traceback (most recent call last) <ipython-input-190-34cd0916eb6a> in <module>() 2 df1, 3 right_index=True, ----> 4 left_index =True) 5 merged_df_energy.info() C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\tools\merge.py
在merge中(left,right,how,on,left_on,right_on,left_index,right_index,sort,suffixes,copy,indicator)37 right_index = right_index,sort = sort,后缀=后缀,38 copy = copy,indicator = indicator)- -> 39如果debug:41 merge,则返回op.get_result()40 。doc = _merge_doc%'\ nleft:DataFrame'
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\tools\merge.py
在get_result(self)215 self.left,self.right)216-> 217 join_index,left_indexer,right_indexer = self._get_join_info()218219 ldata,rdata = self.left._data,self.right._data
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\tools\merge.py
在_get_join_info(self)中337如果self.left_index和self.right_index:338 join_index,left_indexer,right_indexer = \-> 339 left_ax.join(right_ax,how = self.how,return_indexers = True)340 elif self.right_index和self .how =='左':341 join_index,left_indexer,right_indexer = \
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\tseries\index.py
在join(self,other,how,level,return_indexers)1072 this,other = self._maybe_utc_convert(other)1073 return Index.join(this,other,how = how,level = level,-> 1074 return_indexers = return_indexers)1075 1076 def _maybe_utc_convert(self,other):
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py
在join(self,other,how,level,return_indexers)中2480 this = self.astype('O')2481 other = other.astype('O')-> 2482返回this.join(other,how = how,how,return_indexers = return_indexers)2483 2484 _validate_join_method(如何)
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py
在join(self,other,how,level,return_indexers)中2493 else:2494 return self._join_non_unique(other,how = how,-> 2495 return_indexers = return_indexers)2496 elif self.is_monotonic和other.is_monotonic:2497试试:
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\indexes\base.py
在_join_non_unique(自身,其他方式,return_indexers)中2571 left_idx,right_idx = _get_join_indexers([self.values],2572 [other._values],how = how,-> 2573 sort = True)2574 2575 left_idx = com._ensure_platform_int( left_idx)
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\tools\merge.py
在_get_join_indexers(left_keys,right_keys,sort,how)中544 545#获取左右连接标签和num。每个位置的级别数-> 546 llab,rlab,形状= map(list,zip(* map(fkeys,left_keys,right_keys)))547548#从标签列表中获取平坦的i8键
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\tools\merge.py
_factorize_keys(lk,rk,sort)中的718如果排序:719 uniques = rizer.uniques.to_array()-> 720 llab,rlab = _sort_labels(uniques,llab,rlab)721722#NA组
C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas\tools\merge.py
在_sort_labels(唯一,左,右)中741 unique =索引(唯一).values 742-> 743 sorter = unique.argsort()744745 reverse_indexer = np.empty(len(sorter),dtype = np.int64)
pandas\tslib.pyx in pandas.tslib._Timestamp.__richcmp__ (pandas\tslib.c:18619)() TypeError: Cannot compare type 'Timestamp' with type 'int'
您能帮我解决这个问题吗?
谢谢
试试这个:
import pandas
result = pandas.merge(df_energy, df1, on='TIMESTAMP')
如果要保存:
result.to_csv(path_or_buf='result.csv', sep=',')
或检查列:
result_fields = result.columns.tolist()
print (result_fields)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句