简体   繁体   English

在python中匹配两个pandas数据帧的列名

[英]Matching the column names of two pandas data-frames in python

I have two pandas dataframes with names df1 and df2 such that ` 我有两个名为df1df2 pandas数据帧,以便`

df1: a      b    c    d
     1      2    3    4
     5      6    7    8

and

df2:      b    c   
          12   13   

I want the result be like 我希望结果如此

result:    b    c    
           2    3    
           6    7    

Here it should be noted that abcd are the column names in pandas dataframe. 这里应该注意abcd是pandas dataframe中的列名。 The shape and values of both pandas dataframe are different. 两个pandas数据帧的形状和值是不同的。 I want to match the column names of df2 with that of column names of df1 and select all the rows of df1 the headers of which are matched with the column names of df2 .. df2 is only used to select the specific columns of df1 maintaining all the rows. 我想将df2的列名与df1的列名匹配,并选择df1的所有行,其头部与df2的列名匹配df2仅用于选择df1的特定列,保持所有行。 I tried some code given below but that gives me an empty index. 我尝试了下面给出的一些代码,但这给了我一个空索引。

df1.columns.intersection(df2.columns)

The above code is not giving me my resut as it gives index headers with no values. 上面的代码没有给我我的支持,因为它给索引标题没有值。 I want to write a code in which I can give my two dataframes as input and it compares the columns headers for selection. 我想编写一个代码,在其中我可以将我的两个数据帧作为输入,并比较列标题以供选择。 I don't have to hard code column names. 我没有硬编码列名。

I believe you need: 我相信你需要:

df = df1[df1.columns.intersection(df2.columns)]

Or like @Zero pointed in comments: 或者像@Zero在评论中指出:

df = df1[df1.columns & df2.columns]

Or, use reindex 或者,使用reindex

In [594]: df1.reindex(columns=df2.columns)
Out[594]:
   b  c
0  2  3
1  6  7

Also as 也作为

In [595]: df1.reindex(df2.columns, axis=1)
Out[595]:
   b  c
0  2  3
1  6  7

交叉路口:

df = df1[df1.columns.isin(df2.columns)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas:比较一列中的两个数据帧,并返回另一个数据帧中两个数据帧的行内容 - Python Pandas : compare two data-frames along one column and return content of rows of both data frames in another data frame 如何在 Python Pandas 中合并此数据帧? - How to Merge this Data-frames in Python Pandas? 比较两个数据帧,只获取索引和列名不匹配的值 - compare two data frames and get only non matching values with index and column names pandas dataframe python 如何在 pandas 中连接两个具有不同列名的数据帧? - python - how to concat two data frames with different column names in pandas? - python 将两个不同大小的熊猫数据帧逐元素相乘,并添加一个维度作为附加索引/列 - Multiplication of two pandas data-frames of different size element-wise and add a dimension as an additional index/column Pandas:基于一个公共列组合两个不同形状的数据帧 - Pandas: Combine two data-frames with different shape based on one common column 熊猫基于相似的密钥划分两个数据帧 - Pandas Divide two data-frames based on similar keys 可以使用比较来合并两个熊猫数据框吗? - Can one use comparisons to merge two pandas data-frames? 将两个 Pandas 数据帧加在一起时出现关键错误 - Key error adding two Pandas data-frames together 比较具有不同列名的两个数据框,并使用来自第二个数据框的列更新第一个数据框 - Compare two data-frames with different column names and update first data-frame with the column from second data-frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM