0

我正在尝试使用 Pandas 删除包含缺失种族信息的行,尽管我对 Pandas 并不陌生。

使用 'print name[ethnic.isnull() == True]' 我可以想象哪些人缺少种族信息。但最终我想 1)通过将缺失种族案例的索引附加到“缺失数组”中来记录索引,2)然后通过删除索引与“缺失”数组中的行匹配的所有行来创建第二帧。

我目前被困在“for case in frame”循环中,我尝试打印那些缺少种族的人的名字。但是我的程序没有错误地结束,但没有打印出任何东西。

import pandas as pd
from pandas import DataFrame, Series
import numpy as np

### Remove cases with missing name or missing ethnicity information
def RemoveMissing():
    data = pd.read_csv("C:\...\sample.csv")
    frame = DataFrame(data)
    frame.columns = ["Name", "Ethnicity", "Event_Place", "Birth_Place", "URL"]

    missing = []
    name = frame.Name
    ethnic = frame.Ethnicity

    # Filter based on some variable criteria
    #print name[ethnic == "English"]
    #print name[ethnic.isnull() == True] # identify those who don't have ethnicity entry

    # This works
    for case in frame:
        print frame.Name

    # Doesn't work
    for case in frame:
        if frame.Ethnicity.isnull() is True:
            print frame.Name

RemoveMissing()
4

1 回答 1

0

这似乎有效:

# Create a var to check if Ethnicity is missing
index_missEthnic = frame.Ethnicity.isnull()
frame2 = frame[index_missEthnic != True]
于 2014-11-18T06:38:56.080 回答