Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
Postgresql 删除组内的冗余条目_Postgresql_Pandas_Group By_Duplicates - Fatal编程技术网

Postgresql 删除组内的冗余条目

Postgresql 删除组内的冗余条目,postgresql,pandas,group-by,duplicates,Postgresql,Pandas,Group By,Duplicates,我希望在每个组(在本例中为datasource)中删除数据库中的冗余行,我将这些行定义为包含严格少于其他行的信息或与其他行不同的信息的行 例如,在下表中。行1是冗余的,因为其同一组中的另一行0包含与其完全相同的信息,但包含更多数据 出于同样的原因,第6行是冗余的,组中的所有其他第3、4和5行都包含它需要的更多信息。但是,我保留了第4行和第5行,因为它们与组中的其他行相比有一些额外的不同信息 datasource city country 0 1 S

我希望在每个组(在本例中为datasource)中删除数据库中的冗余行,我将这些行定义为包含严格少于其他行的信息或与其他行不同的信息的行

例如,在下表中。行1是冗余的,因为其同一组中的另一行0包含与其完全相同的信息,但包含更多数据

出于同样的原因,第6行是冗余的,组中的所有其他第3、4和5行都包含它需要的更多信息。但是,我保留了第4行和第5行,因为它们与组中的其他行相比有一些额外的不同信息

   datasource         city country
0           1    Shallotte      US
1           1         None      US
2           2       austin      US
3           3  Casselberry      US
4           3         None      AU
5           3  Springfield    None
6           3         None    None
例如,当有更多列时,行0和1、4是不同的信息。但是,第2行和第3行(或第1行)包含冗余信息

  datasource         city country   Count
0           1        None       US     11
1           1       austin    None   None
2           1        None     None     11
3           1       austin    None   None
4           1        None       CA   None
预期产量

  datasource         city country   Count
0           1        None       US     11
1           1       austin    None   None
4           1        None       CA   None

有没有一种简单的方法可以在pandas或SQL(PostrgeSQL)中为任意数量的列实现这种逻辑?

其中一种方法是基于无计数并删除最大无值的行,即

#Count the None values across the row
df['Null'] = (df.values == 'None').sum(axis=1)

#Get the maximum of the count based on groupby
df['Max'] = df.groupby('datasource')['Null'].transform(max)

# Get the values are not equal to max and  equal to zero and drop the columns
df = df[~((df['Max'] !=0) & (df['Max'] == df['Null']))].drop(['Null','Max'],axis=1)
输出:

datasource city country 0 1 Shallotte US 2 2 austin US 3 3 Casselberry US 4 3 None AU 5 3 Springfield None 数据源城市国家 01小葱美国 2美国奥斯汀 3卡塞尔贝里美国酒店 4.3非盟 5 3斯普林菲尔德无
希望能有所帮助

这里有一个不同的方法,使用与Bharath shetty的解决方案相同的基本策略。这样我觉得有点整洁

首先,构建示例数据框:

import pandas as pd
data = {"datasource": [1,1,2,3,3,3,3],
        "city": ["Shallotte", None, "austin", "Casselberry", None, "Springfield", None],
        "country": ["US", "US", "US", "US", "AU", None, None]}
df = pd.DataFrame(data)

df['null'] = df.isnull().sum(axis=1)

print(df)
          city country  datasource  null
0    Shallotte      US           1     0
1         None      US           1     1
2       austin      US           2     0
3  Casselberry      US           3     0
4         None      AU           3     1
5  Springfield    None           3     1
6         None    None           3     2
现在,使用
groupby
apply
创建一个布尔掩码-我们只需删除每组的最大空值:

def null_filter(d):
    if len(d) > 1:
        return d.null < d.null.max()
    return d.null == d.null

mask = df.groupby("datasource").apply(null_filter).values

df.loc(mask).drop("null", 1)

谢谢,但是当有更多的专栏时,这不起作用,我举了一个例子,如果我的问题一开始不清楚,对不起,先生,您对冗余数据的想法有点难以理解。你能添加你想要的预期输出吗。
             city country  datasource
0    Shallotte      US           1
2       austin      US           2
3  Casselberry      US           3
4         None      AU           3
5  Springfield    None           3