【问题标题】:Merge two dataframes with python用python合并两个数据框
【发布时间】:2016-08-19 06:12:18
【问题描述】:

我有两个数据框: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
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

我尝试根据 dfDepas 数据帧中的索引 ['day_of_week'] 合并这两个数据帧。 我不知道如何使用它:merged_df = pd.merge(dfDepas, df7, how='inner',on=['day_of_week'])

有什么好办法帮帮我吗? 谢谢

亲切的问候

编辑

dfDepas
day_of_week P_ACT_KW P_SOUSCR depassement 
Fri 157.258929 427.142857 0.0 
Mon 157.788110 426.875000 0.0 
Sat 166.989236 426.875000 0.0 
Sun 149.676215 426.875000 0.0 
Thurs 157.339286 427.142857 0.0 
Tues 151.122913 427.016021 0.0 
Weds 159.569444 427.142857 0.0


df7

ACT_TIME_AERATEUR_1_F1 ACT_TIME_AERATEUR_1_F3 ACT_TIME_AERATEUR_1_F5 ACT_TIME_AERATEUR_1_F6 ACT_TIME_AERATEUR_1_F7 ACT_TIME_AERATEUR_1_F8

Fri 0.326258 0.330253 0.791144 0.654682 3.204544 1.008550 
Sat -0.201327 -0.228196 0.044616 0.184003 -0.579214 0.292886 
Sun 5.068735 5.250199 5.407271 5.546657 7.823564 5.786713 
Mon -0.587129 -0.559986 -0.294890 -0.155503 2.013379 -0.131496 
Tues-1.244922 -1.510025 -0.788717 -1.098790 -0.996845 -0.718881 
Weds-3.264598 -3.391776 -3.188409 -3.041306 -4.846189 -4.668533 
Thurs -0.178179 0.011002 -1.907544 -2.084516 -6.119337 

【问题讨论】:

    标签: python pandas merge


    【解决方案1】:

    您可以使用reset_index 并将列0 重命名为day_of_week 进行匹配:

    merged_df = pd.merge(dfDepas, 
                         df7.reset_index().rename(columns={0:'day_of_week'}),
                         on=['day_of_week'])
    

    感谢Quickbeam2k1 提供另一个解决方案:

    merged_df = pd.merge(dfDepas.set_index('day_of_week'), 
                         df7,
                         right_index=True,
                         left_index =True)
    

    【讨论】:

    • 你的速度快一点。另一种方法应该是: dfDepas.set_index("day_of_week") 并通过索引合并
    • @jezrael 非常感谢你一直帮助我,但这次我在 _getitem_column(self , key) 2002 # get column 2003 if self.columns.is_unique: -> 2004 return self._get_item_cache(key) 2005 2006 # 重复列和可能的降维 C:\Users\Demonstrator\Anaconda3\lib\site-packages\pandas \core\generic.py in _get_item_cache(self, item) 1348 res = cache.get(item)
    • @CyrineEzzahra - 你能添加一些数据样本吗? 4-5行?因为现在没有数据有点复杂。
    • @jezrael 你能看到我编辑的帖子吗?谢谢
    • @Quickbeam2k1 和 jezrael 非常感谢您的帮助!亲切的问候
    猜你喜欢
    • 2019-07-26
    • 2019-04-28
    • 2014-03-23
    • 1970-01-01
    • 2020-08-16
    • 2012-10-08
    • 2017-08-15
    • 2021-09-10
    相关资源
    最近更新 更多