python:drop_duplicates(subset ='col_name',inplace = True),为什么不能删除某些行?

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

我将通过一列删除重复项,但是可以删除某些行。

有线连接的是:如果我直接读取2个文件而不是我的func1,func2,然后应用放置功能,那么一切都很好!

有没有人可以帮助您?谢谢!这是我的代码:

def func1(file):
    df1 = pd.read_csv('balba')
    """select the col_name, then replace ' ' with ''
    """
    return df1

def func2(file):
    df2 = pd.read_csv('balba')
    """select the col_name, then replace ' ' with '', then rename the column name
    """
    turn df2


df2 = func2(file_df2)

DF1 = []
for i in ['one_file_this_time']:
    d = func1(i)
    DF1.append(d)
df1 = pd.concat([DF1], sort=False)
df1.drop_duplicates(inplace=True)

df = pd.concat([df1, df2], sort=False)
print(df.shape)
# (7749, 2)

df.drop_duplicates(subset='col_name', inplace=True)
print(df.shape)
# (5082, 2)
"""obviously the drop_duplicates() functions works, but not fullly"""

在放置函数之前,连接的数据是(我将其存储为csv格式):enter image description here

下降后功能enter image description here

python pandas subset drop-duplicates
1个回答
0
投票

您应该考虑在here中提到的drop_duplicates方法中添加keep参数。>

现在,您的代码符合以下原则:

第一个:删除第一个重复项之外的重复项。

没有重复的合并可能会为您提供帮助。这个问题与Pandas merge creates unwanted duplicate entries

非常相似
© www.soinside.com 2019 - 2024. All rights reserved.