将 Pandas 数据帧缩减为其他数据帧

Reduction the Pandas dataframe to other dataframe

我有两个数据框,它们的形状是 (707,140) 和 (34,98).

我想根据相同的索引名称和列名称将较大的数据框最小化为较小的数据框。

所以在从较大的数据帧中删除额外的行和列之后,最终它的形状应该是 (34,98) 具有与小数据帧相同的索引和列。

如何在 python 中执行此操作?

我认为你可以 select by loc index and columns of small DataFrame:

dfbig.loc[dfsmall.index, dfsmall.columns]

样本:

dfbig = pd.DataFrame({'a':[1,2,3,4,5], 'b':[4,7,8,9,4], 'c':[5,0,1,2,4]})
print (dfbig)
   a  b  c
0  1  4  5
1  2  7  0
2  3  8  1
3  4  9  2
4  5  4  4

dfsmall = pd.DataFrame({'a':[4,8], 'c':[0,1]})
print (dfsmall)
   a  c
0  4  0
1  8  1

print (dfbig.loc[dfsmall.index, dfsmall.columns])
   a  c
0  1  5
1  2  0