簡體   English   中英

如何從 dataframe 中刪除具有某些值的行?

[英]How can I drop rows with certain values from a dataframe?

我正在獲取兩個不同的數據集並將它們合並到一個數據框中,但我需要獲取結果數據框中的一列(“Presunto Responsable”)並刪除其中包含值“Desconocido”的行。

到目前為止,這是我的代碼:

#%% Get data

def getData(path_A, path_B):
    victims = pd.read_excel(path_A)
    dfv = pd.DataFrame(data=victims)
    cases = pd.read_excel(path_B)
    dfc = pd.DataFrame(data=cases)
    return dfv, dfc

#%% merge dataframes

def mergeData(data_A, data_B):
    data = pd.DataFrame()
    #merge dataframe avoiding duplicated colums
    cols_to_use = data_B.columns.difference(data_A.columns)  
    data = pd.merge(data_A, data_B[cols_to_use], left_index=True, right_index=True, how='outer') 
    cols_at_end = ['Presunto Responsable']
    #Take 'Presunto Responsable' at the end of the dataframe
    data = data[[c for c in data if c not in cols_at_end] 
    + [c for c in cols_at_end if c in data]]
    return data

#%% Drop 'Desconocido' values in 'Presunto Responsable'

def dropData(data):
    indexNames = data[data['Presunto Responsable'] == 'Desconocido'].index
    for c in indexNames:
    data.drop(indexNames , inplace=True)
    return data

生成的 dataframe 中仍然包含具有“Desconocido”值的行。 我究竟做錯了什么?

你可以說:

data = data[data['Presunto Responsable'] != 'Desconocido']

另外,順便說一句,當您執行pd.read_excel()時,它會創建一個 dataframe,您無需將其傳遞給pd.DataFrame()

暫無
暫無

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

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