pandas 如何获取两个pandas数据帧的公共索引?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48170867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the common index of two pandas dataframes?
提问by astudentofmaths
I have two pandas DataFrames df1 and df2 and I want to transform them in order that they keep values only for the index that are common to the 2 dataframes.
我有两个 Pandas DataFrames df1 和 df2,我想对它们进行转换,以便它们仅保留 2 个数据帧共有的索引的值。
df1
df1
values 1
0
28/11/2000 -0.055276
29/11/2000 0.027427
30/11/2000 0.066009
01/12/2000 0.012749
04/12/2000 0.113892
df2
df2
values 2
24/11/2000 -0.004808
27/11/2000 -0.001812
28/11/2000 -0.026316
29/11/2000 0.015222
30/11/2000 -0.024480
become
变得
df1
df1
value 1
28/11/2000 -0.055276
29/11/2000 0.027427
30/11/2000 0.066009
df2
df2
value 2
28/11/2000 -0.026316
29/11/2000 0.015222
30/11/2000 -0.024480
回答by jezrael
You can use Index.intersection
+ DataFrame.loc
:
您可以使用Index.intersection
+ DataFrame.loc
:
idx = df1.index.intersection(df2.index)
print (idx)
Index(['28/11/2000', '29/11/2000', '30/11/2000'], dtype='object')
Alternative solution with numpy.intersect1d
:
替代解决方案numpy.intersect1d
:
idx = np.intersect1d(df1.index, df2.index)
print (idx)
['28/11/2000' '29/11/2000' '30/11/2000']
df1 = df1.loc[idx]
print (df1)
values 1
28/11/2000 -0.055276
29/11/2000 0.027427
30/11/2000 0.066009
df2 = df2.loc[idx]
回答by MaxU
In [352]: common = df1.index.intersection(df2.index)
In [353]: df1.loc[common]
Out[353]:
values1
0
28/11/2000 -0.055276
29/11/2000 0.027427
30/11/2000 0.066009
In [354]: df2.loc[common]
Out[354]:
values2
0
28/11/2000 -0.026316
29/11/2000 0.015222
30/11/2000 -0.024480
回答by Zero
And, using isin
. intersection
might be faster though.
并且,使用isin
. intersection
不过可能会更快。
In [286]: df1.loc[df1.index.isin(df2.index)]
Out[286]:
values1
0
28/11/2000 -0.055276
29/11/2000 0.027427
30/11/2000 0.066009
In [287]: df2.loc[df2.index.isin(df1.index)]
Out[287]:
values2
0
28/11/2000 -0.026316
29/11/2000 0.015222
30/11/2000 -0.024480
回答by YOBEN_S
reindex
+ dropna
reindex
+ dropna
df1.reindex(df2.index).dropna()
Out[21]:
values1
28/11/2000 -0.055276
29/11/2000 0.027427
30/11/2000 0.066009
df2.reindex(df1.index).dropna()
Out[22]:
values2
28/11/2000 -0.026316
29/11/2000 0.015222
30/11/2000 -0.024480
回答by iamjli
The index object has some set-like properties so you simply can take the intersection as follows:
索引对象具有一些类似集合的属性,因此您只需按如下方式取交集即可:
df1 = df1.reindex[ df1.index & df2.index ]
This retains the order of the first dataframe in the intersection, df
.
这保留了交集中第一个数据帧的顺序,df
。
回答by LangeHaare
Have you tried something like
你有没有尝试过类似的东西
df1 = df1.loc[[x for x in df1.index if x in df2.index]]
df2 = df2.loc[[x for x in df2.index if x in df1.index]]
回答by starostise
You can pd.mergethem with an intermediary DataFrame created with the indexes of the other DataFrame:
您可以pd.merge与其他数据框的索引创建的中介数据帧他们:
df2_indexes = pd.DataFrame(index=df2.index)
df1 = pd.merge(df1, df2_indexes, left_index=True, right_index=True)
df1_indexes = pd.DataFrame(index=df1.index)
df2 = pd.merge(df2, df1_indexes, left_index=True, right_index=True)
or you can use pd.eval:
或者你可以使用pd.eval:
df2_indexes = df2.index.values
df1 = df1[eval("df1.index in df2_indexes"]
df1_indexes = df1.index.values
df2 = df2[eval("df2.index in df1_indexes"]