python - 如果 None 或 nan 则合并 pandas 中的两行

标签 python pandas nan pandas-groupby

对于给定的数据框df

df = pd.DataFrame({
    'id': [1, 2, 2], 
    'name': ['Peter', 'Max', None], 
    'age': [50.0, np.nan, 60.0]
})

如果分组行的列中只有 Nonenan,我想要groupby 并合并数据,以便结果 df 应该看起来像

        age     id  name
id              
1   0   50.0    1   Peter
2   1   60.0    2   Max

有没有比我的这个更好的简洁解决方案:

def f(df):
    names = set(df['name']) - {None}
    if len(names) == 1:
        df['name'] = names.pop()
    else:
        print('Error: Names are not mergeable:', names)

    ages = {age for age in df['age'] if ~np.isnan(age)}
    if len(ages) == 1:
        df['age'] = ages.pop()
    else:
        print('Error: Ages are not mergeable:', ages)

    df = df.drop_duplicates()
    return df

df.groupby('id').apply(f)

最佳答案

这可能是最慢的解决方案,您可以将 nan 排序到最后并将它们放入 groupby 中,即

df = pd.DataFrame({
    'id': [1, 2, 2,1,2], 
    'name': ['Peter', 'Max', None,'Daniel','Sign'], 
    'age': [50.0, np.nan, 60.0,40,30]
})
#    age  id    name
#0  50.0   1   Peter
#1   NaN   2     Max  
#2  60.0   2    None
#3  40.0   1  Daniel
#4  30.0   2    Sign

df.groupby('id').apply(lambda x: x.apply(sorted,key=pd.isnull).dropna()).reset_index(drop=True)

    age  id    name
0  50.0   1   Peter
1  40.0   1  Daniel
2  60.0   2     Max
3  30.0   2    Sign

关于python - 如果 None 或 nan 则合并 pandas 中的两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48284040/

相关文章:

python - 根据 xlsxwriter 中的列对数据进行排序

python - matplotlib 中的多行 x 刻度标签

python - 检查需要删除多少个字符才能在 Python 中生成字谜

python - 打印从文本导入的表格时标题未对齐

python - 在python中从另一个列表中删除一个列表中存在的项目

python - Pandas 根据另一列的条件申请

python - 在 Python 中结合使用 argparse 和 sys.argv

python - Pandas:使用 `map` 进行左合并返回 NaN

debugging - Fortran 输出 NaN 的所有情况是什么?

python - 使用 NaN 按列对数据进行 Winsorizing