我正在获取两个不同的数据集并将它们合并到一个数据框中,但我需要获取生成的数据框的其中一列(“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
生成的数据帧中仍然包含带有“Desconocido”值的行。我做错了什么?
答案 0 :(得分:1)
你可以说:
data = data[data['Presunto Responsable'] != 'Desconocido']
另外,顺便说一句,当您执行 pd.read_excel()
时,它会创建一个数据框,您无需将其传递给 pd.DataFrame()
。