当前位置: 首页 > 知识库问答 >
问题:

Python熊猫-删除nans[重复]的问题

令狐宣
2023-03-14

我正在努力去除nans。已经花了一些时间寻找解决方案,但似乎没有任何效果。

下面我附上我的代码样本。整个笔记本可以在我的GitHub这里找到:https://GitHub . com/jarsonX/Temp _ files/blob/main/W3-探索性数据分析(1)。ipynb

import pandas as pd     
import seaborn as sns               #not used in this sample, needed for plotting later on
import matplotlib as mpl            #as above
import matplotlib.pyplot as plt     #as above
import numpy as np                  #as above

df = pd.read_csv("https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DA0321EN-SkillsNetwork/LargeData/m2_survey_data.csv")

df.Age.describe()  #dtype float64

df['Age'].isna().value_counts()  #287 nans

df['Age'].dropna(how='any', inplace=True)  #trying to remove nans

df['Age'].isna().value_counts()  #still 287 nans

#Just for the sake of identification of rows
#I tried to print ONLY nans but could not figure out how to do it.
i = 0
for el in df.Age:
    print(i, el, type(el))
    i += 1

#The first nan is in the 67th row

我错过了什么?

更新:

我设法过滤掉了南斯:

i = 0
for el in df.Age:
    if el != el:
        print(i, el, type(el))
    i += 1

共有2个答案

陈寒
2023-03-14

df=df[~df['年龄']. isnull()]

df['Age'].isna().value_counts()

钱朝明
2023-03-14

您可以尝试以下代码片段,在系列中调用 dropna 时不考虑 how 参数,因为它只是一列

< code>df.dropna(subset=["Age"],how="any ",inplace=True)

 类似资料: