熊猫:删除所有NaN的列

the*_*ler 6 python in-place dataframe pandas

我意识到NaN从数据帧中删除s很简单,df.dropna但是由于某种原因,它对我的工作不起作用,我不确定为什么。

这是我的原始数据框:

fish_frame1:                       0   1   2         3   4       5   6          7
0               #0915-8 NaN NaN       NaN NaN     NaN NaN        NaN
1                   NaN NaN NaN  LIVE WGT NaN  AMOUNT NaN      TOTAL
2               GBW COD NaN NaN     2,280 NaN   $0.60 NaN  $1,368.00
3               POLLOCK NaN NaN     1,611 NaN   $0.01 NaN     $16.11
4                 WHAKE NaN NaN       441 NaN   $0.70 NaN    $308.70
5           GBE HADDOCK NaN NaN     2,788 NaN   $0.01 NaN     $27.88
6           GBW HADDOCK NaN NaN    16,667 NaN   $0.01 NaN    $166.67
7               REDFISH NaN NaN       932 NaN   $0.01 NaN      $9.32
8    GB WINTER FLOUNDER NaN NaN       145 NaN   $0.25 NaN     $36.25
9   GOM WINTER FLOUNDER NaN NaN    25,070 NaN   $0.35 NaN  $8,774.50
10        GB YELLOWTAIL NaN NaN        26 NaN   $1.75 NaN     $45.50
Run Code Online (Sandbox Code Playgroud)

以下代码尝试删除all NaN以及任何超过3 NaNs的列(我认为一个或两个都应该起作用):

fish_frame.dropna()
fish_frame.dropna(thresh=len(fish_frame) - 3, axis=1)
Run Code Online (Sandbox Code Playgroud)

这将产生:

fish_frame1 after dropna:                       0   1   2         3   4       5   6          7
0               #0915-8 NaN NaN       NaN NaN     NaN NaN        NaN
1                   NaN NaN NaN  LIVE WGT NaN  AMOUNT NaN      TOTAL
2               GBW COD NaN NaN     2,280 NaN   $0.60 NaN  $1,368.00
3               POLLOCK NaN NaN     1,611 NaN   $0.01 NaN     $16.11
4                 WHAKE NaN NaN       441 NaN   $0.70 NaN    $308.70
5           GBE HADDOCK NaN NaN     2,788 NaN   $0.01 NaN     $27.88
6           GBW HADDOCK NaN NaN    16,667 NaN   $0.01 NaN    $166.67
7               REDFISH NaN NaN       932 NaN   $0.01 NaN      $9.32
8    GB WINTER FLOUNDER NaN NaN       145 NaN   $0.25 NaN     $36.25
9   GOM WINTER FLOUNDER NaN NaN    25,070 NaN   $0.35 NaN  $8,774.50
10        GB YELLOWTAIL NaN NaN        26 NaN   $1.75 NaN     $45.50
Run Code Online (Sandbox Code Playgroud)

我是的新手,Pandas所以我不确定这是行不通的,因为我做错了什么,或者我误解了某些事情或滥用了命令。任何帮助表示赞赏,谢谢。

Cor*_*man 19

dropna文档字符串:

    # drop the columns where all elements are NaN:

    >>> df.dropna(axis=1, how='all')
         A    B  D
    0  NaN  2.0  0
    1  3.0  4.0  1
    2  NaN  NaN  5
Run Code Online (Sandbox Code Playgroud)

  • (已投票)。如果要保留没有空列的新数据框,请使用“inplace = True”选项。例如: df.dropna(axis=1, how='all', inplace=True) (2认同)

Rak*_*van 8

dropna()删除空值并返回一个数据帧。将其分配回原始数据帧。

fish_frame = fish_frame.dropna(axis = 1, how = 'all')
Run Code Online (Sandbox Code Playgroud)

参考你的代码:

fish_frame.dropna(thresh=len(fish_frame) - 3, axis=1)
Run Code Online (Sandbox Code Playgroud)

这将删除具有 7 个或更多 NaN 的列(假设 len(df) = 10),如果您想像您提到的那样删除具有 3 个以上 Nan 的列,则 thresh 应等于 3。


See*_*eer 8

dropna()默认情况下返回一个数据帧(默认为inplace=False行为),因此需要将其分配给一个新的数据帧以使其保留在代码中。

例如,

fish_frame = fish_frame.dropna()
Run Code Online (Sandbox Code Playgroud)

至于为什么你dropna返回一个空数据帧,我建议你查看 dropna 方法中的“how”参数(https://pandas.pydata.org/pandas-docs/stable/ generated/pandas.DataFrame.dropna .html )。另请记住,axis=0 对应于列,axis=1 对应于行。

因此,要删除所有“NA”的列,axis=0,how=“any”应该可以解决问题:

fish_frame = fish_frame.dropna(axis=0, how="any")
Run Code Online (Sandbox Code Playgroud)

最后,“thresh”参数明确指定需要多少个 NA 才能发生丢弃。所以

fish_frame = fish_frame.dropna(axis=0, thresh=3, how="any") 
Run Code Online (Sandbox Code Playgroud)

应该可以很好地删除任何具有三个 NA 的列。

另外,正如 Corley 指出的那样,how="any" 是默认值,因此不是必需的。


Ach*_*age 8

另一种解决方案是创建一个在非空位置具有 True 值的布尔数据框,然后获取至少具有一个 True 值的列。下面的行删除包含所有 NaN 值的列。

df = df.loc[:,df.notna().any(axis=0)]
Run Code Online (Sandbox Code Playgroud)

如果要删除至少有一个缺失 (NaN) 值的列;

df = df.loc[:,df.notna().all(axis=0)]
Run Code Online (Sandbox Code Playgroud)

此方法在删除包含空字符串、零或基本上任何给定值的列时特别有用。例如;

df = df.loc[:,(df!='').all(axis=0)]
Run Code Online (Sandbox Code Playgroud)

删除至少有一个空字符串的列。