如何获得两个pandas数据帧的通用索引?

ast*_*ths 12 python dataframe pandas

我有两个pandas DataFrames df1和df2,我想转换它们,以便它们仅为2个数据帧共有的索引保留值.

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

成为

DF1

                     value 1

28/11/2000          -0.055276
29/11/2000           0.027427
30/11/2000           0.066009
Run Code Online (Sandbox Code Playgroud)

DF2

                       value 2

28/11/2000            -0.026316
29/11/2000             0.015222
30/11/2000            -0.024480
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 17

你可以使用Index.intersection+ DataFrame.loc:

idx = df1.index.intersection(df2.index)
print (idx)
Index(['28/11/2000', '29/11/2000', '30/11/2000'], dtype='object')
Run Code Online (Sandbox Code Playgroud)

替代解决方案numpy.intersect1d:

idx = np.intersect1d(df1.index, df2.index)
print (idx)
['28/11/2000' '29/11/2000' '30/11/2000']
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)


Max*_*axU 7

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
Run Code Online (Sandbox Code Playgroud)


Zer*_*ero 6

并且,使用isinintersection可能会更快。

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
Run Code Online (Sandbox Code Playgroud)