执行涉及索引和列的熊猫连接的最简洁方法
Posted
技术标签:
【中文标题】执行涉及索引和列的熊猫连接的最简洁方法【英文标题】:Cleanest way to perform pandas join involving an index and also columns 【发布时间】:2014-11-21 22:54:03 【问题描述】:假设我想通过连接一个未命名的索引和一个列(这里是“标识符”)来合并(连接?)两个 pandas 表。有没有一种干净的方法可以做到这一点?
tvType identifier
2014-04-08 12:05:00 TMM_ISPREAD ISIN: US92344GAS57
2014-04-08 12:10:00 TMM_ISPREAD ISIN: US92344GAS57
2014-04-08 12:15:00 TMM_ISPREAD ISIN: US92344GAS57
2014-04-08 12:20:00 TMM_ISPREAD ISIN: US92344GAS57
2014-04-08 12:20:00 OTHER ISIN: US92344GAS75
tv identifier
2014-04-08 12:05:00 131.957086 ISIN:US92344GAS57
2014-04-08 12:10:00 131.932117 ISIN:US92344GAS57
2014-04-08 12:15:00 131.945260 ISIN:US92344GAS57
2014-04-08 12:20:00 131.888946 ISIN:US92344GAS57
2014-04-08 12:20:00 131.920969 ISIN:US92344GAS75
说清楚,我想要的是
tv identifier tvType
2014-04-08 12:05:00 131.957086 ISIN:US92344GAS57 TMM_ISPREAD
2014-04-08 12:10:00 131.932117 ISIN:US92344GAS57 TMM_ISPREAD
2014-04-08 12:15:00 131.945260 ISIN:US92344GAS57 TMM_ISPREAD
2014-04-08 12:20:00 131.888946 ISIN:US92344GAS57 TMM_ISPREAD
2014-04-08 12:20:00 131.920969 ISIN:US92344GAS75 OTHER
而我看似繁琐的方式是
dfTv['time_index'] = dfTv.index
dfTvType['time_index'] = dfTvType.index
df = pd.merge( dfTv, dfTvType, on = ['identifier','time_index'] )
df = df.set_index(['time_index'])
(命名索引 - 不是我真正想要的副作用)
【问题讨论】:
一个糟糕的剪切和粘贴! 【参考方案1】:也只需提供left_index
和right_index
:
In [96]:
print df1
tvType identifier
2014-04-08 12:05:00 TMM_ISPREAD ISIN:US92344GAS57
2014-04-08 12:10:00 TMM_ISPREAD ISIN:US92344GAS57
2014-04-08 12:15:00 TMM_ISPREAD ISIN:US92344GAS57
2014-04-08 12:20:00 TMM_ISPREAD ISIN:US92344GAS57
2014-04-08 12:25:00 OTHER ISIN:US92344GAS75
In [97]:
print df2
tv identifier
2014-04-08 12:05:00 131.957086 ISIN:US92344GAS57
2014-04-08 12:10:00 131.932117 ISIN:US92344GAS57
2014-04-08 12:15:00 131.945260 ISIN:US92344GAS57
2014-04-08 12:20:00 131.888946 ISIN:US92344GAS57
2014-04-08 12:25:00 131.920969 ISIN:US92344GAS75
In [98]:
print pd.merge(df2, df1, on = ['identifier'], left_index=True, right_index=True)
tv identifier tvType
2014-04-08 12:05:00 131.957086 ISIN:US92344GAS57 TMM_ISPREAD
2014-04-08 12:10:00 131.932117 ISIN:US92344GAS57 TMM_ISPREAD
2014-04-08 12:15:00 131.945260 ISIN:US92344GAS57 TMM_ISPREAD
2014-04-08 12:20:00 131.888946 ISIN:US92344GAS57 TMM_ISPREAD
2014-04-08 12:25:00 131.920969 ISIN:US92344GAS75 OTHER
【讨论】:
以上是关于执行涉及索引和列的熊猫连接的最简洁方法的主要内容,如果未能解决你的问题,请参考以下文章