如何拆分数据框
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
答案 0 :(得分:0)
我相信您可以将Index.isin
与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
与DataFrame.loc
进行标签选择:
print (df.loc[df.index.difference(idx)])
first second
bat 0.544883 0.891773
baz 0.602763 0.437587