如何分割数据框
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
我相信您可以将Index.isin
与Index.isin
一起用作第二个boolean indexing
:
boolean indexing
或DataFrame
与idx = ['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