[英]How to count NaN or missing values in Pandas DataFrame at a particular row level?
[英]How to count NaN values for each row in pandas dataframe?
我想計算 DataFrame 中每一行的 NaN 值,然后獲得此類值的最少數量。 我的解決方案太慢了,而且使用 for 循環也不是 pandas 的方式。 有更好更快的方法嗎?
max_not_nan = 13 # a maximum possible value of NaN's (number of columns + 1)
row_number = 0
for i in range(df.shape[0]):
if df.iloc[i].isna().sum() < max_not_nan:
max_not_nan = df.iloc[i].isna().sum()
row_number = i
它工作正常期望時間復雜度
transactions.isnull().sum(axis=1).sort_values()
你能試試這個嗎:
df['nan_count'] = df.isnull().sum(axis=1) #get nan counts for each row as a new column
max_nan=df[df['nan_count']==df['nan_count'].max()] #get the row with the max nan count
min_nan=df[df['nan_count']==df['nan_count'].min()] #get the row with the min nan count
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.