如何从 df 中删除 nan 而不丢失整行?

G'day,如何在不丢失整行的情况下删除 nan 值?这就是我的样子。df

http://img4.mukewang.com/62e8dd2c000139f207820247.jpg

我已经尝试了但这删除了行中的所有内容。pandasdf = schule.dropna()

但我必须保持这些价值观,因为在最后,我希望它们向上发展。

http://img4.mukewang.com/62e8dd38000138f407350563.jpg


慕姐4208626
浏览 105回答 3
3回答

守着星空守着你

它可能会有所帮助。你可以尝试一下!!!!

天涯尽头无女友

根据问题陈述,您希望降低 nan 值的优先级,并将非 nan 值置于顶部。import numpy as npimport pandas as pdimport functoolsdef drop_and_roll(col, na_position='last', fillvalue=np.nan):    result = np.full(len(col), fillvalue, dtype=col.dtype)    mask = col.notnull()    N = mask.sum()    if na_position == 'last':        result[:N] = col.loc[mask]    elif na_position == 'first':        result[-N:] = col.loc[mask]    else:        raise ValueError('na_position {!r} unrecognized'.format(na_position))    return resultdf = pd.read_table('data', sep='\s{2,}')print(df.apply(functools.partial(drop_and_roll, fillvalue='')))

慕村9548890

假设您要回填值,然后删除任何列中显示的任何重复项,此示例有效:import pandas as pdimport numpy as npdata = [&nbsp; &nbsp; ['POINT_1.1', 'POINT_1.2', pd.NA],&nbsp; &nbsp; [pd.NA, pd.NA, 'POINT_1.3'],&nbsp; &nbsp; ['POINT_2.1', 'POINT_2.2', pd.NA],&nbsp; &nbsp; [pd.NA, pd.NA, 'POINT_2.3']]df = pd.DataFrame(data)df#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2# 0&nbsp; POINT_1.1&nbsp; POINT_1.2&nbsp; &nbsp; &nbsp; &nbsp;<NA># 1&nbsp; &nbsp; &nbsp; &nbsp;<NA>&nbsp; &nbsp; &nbsp; &nbsp;<NA>&nbsp; POINT_1.3# 2&nbsp; POINT_2.1&nbsp; POINT_2.2&nbsp; &nbsp; &nbsp; &nbsp;<NA># 3&nbsp; &nbsp; &nbsp; &nbsp;<NA>&nbsp; &nbsp; &nbsp; &nbsp;<NA>&nbsp; POINT_2.3t = df.T.bfill().T.bfill()t#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2# 0&nbsp; POINT_1.1&nbsp; POINT_1.2&nbsp; POINT_1.3# 2&nbsp; POINT_2.1&nbsp; POINT_2.2&nbsp; POINT_2.3for column in t.columns:&nbsp; &nbsp; t = t.drop_duplicates(column)t#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2# 0&nbsp; POINT_1.1&nbsp; POINT_1.2&nbsp; POINT_1.3# 2&nbsp; POINT_2.1&nbsp; POINT_2.2&nbsp; POINT_2.3
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python