用python合并两个数据帧
我有两个数据帧:dfDepas和df7用python合并两个数据帧,python,pandas,merge,Python,Pandas,Merge,我有两个数据帧:dfDepas和df7 dfDepas.info() <class 'pandas.core.frame.DataFrame'> Int64Index: 7 entries, 0 to 6 Data columns (total 4 columns): day_of_week 7 non-null object P_ACT_KW 7 non-null float64 P_SOUSCR 7 non-null float64 depassem
dfDepas.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 7 entries, 0 to 6
Data columns (total 4 columns):
day_of_week 7 non-null object
P_ACT_KW 7 non-null float64
P_SOUSCR 7 non-null float64
depassement 7 non-null float64
dtypes: float64(3), object(1)
memory usage: 280.0+ bytes
df7.info()
<class 'pandas.core.frame.DataFrame'>
Index: 7 entries, Fri to Thurs
Data columns (total 6 columns):
ACT_TIME_AERATEUR_1_F1 7 non-null float64
ACT_TIME_AERATEUR_1_F3 7 non-null float64
ACT_TIME_AERATEUR_1_F5 7 non-null float64
ACT_TIME_AERATEUR_1_F6 7 non-null float64
ACT_TIME_AERATEUR_1_F7 7 non-null float64
ACT_TIME_AERATEUR_1_F8 7 non-null float64
dtypes: float64(6)
memory usage: 392.0+ bytes
您可以使用列0
并将其重命名为day\u of_week
,以进行匹配:
merged_df = pd.merge(dfDepas,
df7.reset_index().rename(columns={0:'day_of_week'}),
on=['day_of_week'])
感谢您提供另一个解决方案:
merged_df = pd.merge(dfDepas.set_index('day_of_week'),
df7,
right_index=True,
left_index =True)
你只需要再快一点。另一种方法应该是:dfDepas.set_index(“周中的天”)并通过index@jezrael非常感谢您一直以来对我的帮助,但这次我在_getitem_列(self,key)中得到了C:\Users\Demonstrator\Anaconda3\lib\site packages\pandas\core\frame.py2002#如果self.columns.is_unique,则获取column 2003:->2004返回self。#获取项目(key)2005 2006#重复列和可能的降维C:\Users\Demonstrator\Anaconda3\lib\site packages\pandas\core\generic.py in(获取项目)self,item)1348 res=cache.get(item)@CyrineEzzahra-你能添加一些数据样本吗?4-5排?因为现在没有数据有点复杂。@jezrael你能看到我编辑的帖子吗?感谢you@Quickbeam2k1耶斯雷尔非常感谢你的帮助!!问候
merged_df = pd.merge(dfDepas.set_index('day_of_week'),
df7,
right_index=True,
left_index =True)