我的数据帧df是3020x4。 我想删除原始的df1 20x4子集。换句话说,我只是希望得到其形状为3000x4的差异。我尝试了以下但它没有用。它返回的确是df。你能帮忙吗?感谢。
new_df = df.drop(df1)
答案 0 :(得分:6)
由于您似乎无法发布代表性示例,我将使用merge
使用param indicator=True
演示一种方法:
因此生成一些数据:
In [116]:
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
df
Out[116]:
a b c
0 -0.134933 -0.664799 -1.611790
1 1.457741 0.652709 -1.154430
2 0.534560 -0.781352 1.978084
3 0.844243 -0.234208 -2.415347
4 -0.118761 -0.287092 1.179237
选择一个子集:
In [118]:
df_subset=df.iloc[2:3]
df_subset
Out[118]:
a b c
2 0.53456 -0.781352 1.978084
现在使用参数merge
执行左indicator=True
这将添加_merge
列,指示该行是left_only
,both
还是{{1} (后者不会出现在此示例中)并且我们过滤合并的df以仅显示right_only
:
left_only
这是原始的合并df:
In [121]:
df_new = df.merge(df_subset, how='left', indicator=True)
df_new = df_new[df_new['_merge'] == 'left_only']
df_new
Out[121]:
a b c _merge
0 -0.134933 -0.664799 -1.611790 left_only
1 1.457741 0.652709 -1.154430 left_only
3 0.844243 -0.234208 -2.415347 left_only
4 -0.118761 -0.287092 1.179237 left_only
答案 1 :(得分:5)
pandas cheat sheet还建议以下技术
<ion-footer>
<ion-toolbar>
<ion-buttons slot="start">
<ion-button>
<ion-icon slot="icon-only" name="create" color="royal"></ion-icon>
</ion-button>
</ion-buttons>
<ion-buttons slot="end">
<ion-button (click)="displayAdd()">
<ion-icon slot="icon-only" name="add" color="royal"></ion-icon>
</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-footer>
其中x1是要比较的列,adf是从中取出出现在数据框bdf中的相应行的数据框。
OP提出的特定问题也可以通过
解决adf[~adf.x1.isin(bdf.x1)]