簡體   English   中英

我正在嘗試返回缺失值超過 n 個的所有行,我想將它們放在一個新的數據框中

[英]I am trying to return all rows where there is more than n number of missing values, i would like to put these in a new data frame

顯然,新數據框將包含不包含任何缺失值的列。

generic = df.iloc[df['Current Age'] == isnull()]

此代碼不起作用 bc in=snull() 未定義,但基本上,我試圖找到所有缺失值超過 6 個的列,並將它們放入一個名為generic的新數據框中

好吧,讓我們想象一下我們有一個這樣的數據框:

import numpy as np
import pandas as pd
df = pd.DataFrame([1,2,3, np.nan, 5, 3, np.nan], columns = ['value'])

如果參數是 NaN,則 np.isnan() 返回 true,並且當您過濾數據框時,它僅返回根據您的條件為 true 的行。

所以,看下面的代碼:

nanIndex = df[np.isnan(df['value'])].index.tolist() # Rows that contains NaN
newDF = df.drop(index = nanIndex) # Drop that rows, keep only non NaN rows
newDF.reset_index(drop = True, inplace = True) # Reset the index of the dataframe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM