基于行索引在两个不相交的子帧中分割数据帧

问题描述 投票:1回答:1

如何分割数据框

import pandas as pd
import numpy as np
np.random.seed(0)
df = pd.DataFrame({'first':np.random.rand(4),'second':np.random.rand(4)},index=['foo','bar','baz','bat'])
print(df)
        first    second
foo  0.548814  0.423655
bar  0.715189  0.645894
baz  0.602763  0.437587
bat  0.544883  0.891773

进入以下两个不相交的数据帧

        first    second
foo  0.548814  0.423655
bar  0.715189  0.645894

     first    second
baz  0.602763  0.437587
bat  0.544883  0.891773

通过使用第一个数据帧的索引?

我专门在寻找类似的方法

subDf1,subDf2 = pd.split(df,['foo','bar'])

其中

print(subDf1)
   first    second
foo  0.548814  0.423655
bar  0.715189  0.645894

print(subDf2 )
  first    second
baz  0.602763  0.437587
bat  0.544883  0.891773
python pandas
1个回答
0
投票

我相信您可以将Index.isinIndex.isin一起用作第二个boolean indexing

boolean indexing

DataFrameidx = ['foo','bar'] print (df.loc[idx]) first second foo 0.548814 0.423655 bar 0.715189 0.645894 print (df[~df.index.isin(idx)]) first second baz 0.602763 0.437587 bat 0.544883 0.891773 一起通过标签选择:

Index.difference
© www.soinside.com 2019 - 2024. All rights reserved.