[英]Matching the column names of two pandas data-frames in python
I have two pandas dataframes with names df1
and df2
such that ` 我有两个名为
df1
和df2
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.