【问题标题】:Using pd.concat to inner join two dataframes, only selecting columns from one使用 pd.concat 内部连接两个数据框,只从一个中选择列
【发布时间】:2018-02-17 23:58:19
【问题描述】:

我有两个数据框(我们称它们为 df1 和 df2)。我想根据索引执行内连接,但只从 df1.x 中获取列。

在 SQL 中,它是:

Select a.*
From df1 a
Inner join df2 b 
On a.index = b.index 

我在 Python 中的代码是:

pd.concat([df1, df2], axis = 1, join = 'inner', join_axes = [df1.index])

但它会从 df1 和 df2 中选择所有列。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    您可以在 pd.concat 之后使用 [] 的一种方法:

    pd.concat([df1, df2], axis = 1, join = 'inner', join_axes = [df1.index])[df1.columns]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 2019-08-21
      相关资源
      最近更新 更多