Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用另一个数据帧从Python中的数据帧中删除记录_Python_Dataframe - Fatal编程技术网

使用另一个数据帧从Python中的数据帧中删除记录

使用另一个数据帧从Python中的数据帧中删除记录,python,dataframe,Python,Dataframe,我已经为此挣扎了几天了——我读过很多类似的帖子,并使用了它们的代码答案。基本上,我尝试从这个数据帧中过滤(删除)行: Name: df OrderQty InvoiceDate CID 1000363 6 1/20/2020 1002047 1 10/14/2019 1003565 7 10/9/2019 1003680 5 10/2/2019 1010

我已经为此挣扎了几天了——我读过很多类似的帖子,并使用了它们的代码答案。基本上,我尝试从这个数据帧中过滤(删除)行:

Name: df

       OrderQty InvoiceDate 
CID                        
1000363         6   1/20/2020
1002047         1  10/14/2019
1003565         7   10/9/2019
1003680         5   10/2/2019
1010933         1  12/10/2019

[115547 rows x 2 columns]
Type : 'pandas.core.frame.DataFrame'
Index: CID
使用另一个看起来非常相似的数据帧:

Name: df2

       OrderQty InvoiceDate 
CID                        
1000363         6   1/20/2020
1002047         1  10/14/2019
1003565         7   10/9/2019
1003680         5   10/2/2019
1010933         1  12/10/2019

[6387 rows x 2 columns]
Type : 'pandas.core.frame.DataFrame'
Index: CID
列“CID”上的df2(每个CID一行-这些是我想从df中删除的CID)和df(多行中多行具有相同的CID)之间存在一对多关系

到目前为止,我已经尝试了以下方法来尝试删除df中具有相同CID的行:

new_df = df[df['CID']].isin(df2[df2['CID']])
给出一个键错误

cond = df['CID'].isin(df2['CID'])
new_df = df.drop(df[cond].index, inplace = True)
new_df = df['CID'].isin(df2).dropna()
new_df = df[~df['CID'].isin(df2)].dropna()
new_df = df.query('CID not in @df2')
给出一个键错误

cond = df['CID'].isin(df2['CID'])
new_df = df.drop(df[cond].index, inplace = True)
new_df = df['CID'].isin(df2).dropna()
new_df = df[~df['CID'].isin(df2)].dropna()
new_df = df.query('CID not in @df2')
给出一个键错误

cond = df['CID'].isin(df2['CID'])
new_df = df.drop(df[cond].index, inplace = True)
new_df = df['CID'].isin(df2).dropna()
new_df = df[~df['CID'].isin(df2)].dropna()
new_df = df.query('CID not in @df2')
给出一个键错误

cond = df['CID'].isin(df2['CID'])
new_df = df.drop(df[cond].index, inplace = True)
new_df = df['CID'].isin(df2).dropna()
new_df = df[~df['CID'].isin(df2)].dropna()
new_df = df.query('CID not in @df2')
无错误,但不会从df中删除和记录

new_df = df[~df.CID.isin(df2)]
提供AttributeError:“DataFrame”对象没有属性“CID”

我现在非常困惑——我已经关闭/重置了我的python应用程序好几次,使用了我每次阅读的不同帖子中的不同代码片段(如上),没有任何更改。我认为我面临的问题可能是将“CID”作为索引,或者是我正在处理的一对多关系

提前感谢

熊猫在标签上工作,默认情况下,这些标签将是数据帧的索引。在您的例子中,CID是索引,因此这应该像

df_new = df.drop(df2.index)

试着运行:
cond=df['CID'].isin(df2['CID'].values)
new_df=df.drop(df[cond].index,inplace=True)它与您运行的第一个类似,但是根据数组而不是序列检查可能会起作用。不幸的是,我无法让它起作用。我的伙计们,感谢我得到的回复df_new=df.drop(df2.index)工作-令人惊讶的是,这么小的一行代码如何产生如此大的差异!非常感谢你的帮助!