-1

I'm taking two different datasets and merging them into a single data frame, but I need to take one of the columns ('Presunto Responsable') of the resulting data frame and remove the rows with the value 'Desconocido' in it.

This is my code so far:

#%% 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

The resulting dataframe still has the rows with 'Desconocido' values in them. What am I doing wrong?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. We expect a minimal working example of the problem, including appropriate code to trace the internal operation. Your posted code merely defines three functions and quits. – Prune Mar 27 '21 at 21:31
  • We also expect that you will trace the suspect values just before the point of error. Where are you confused about how they got to those values? – Prune Mar 27 '21 at 21:31
  • [Include your minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of the example. – Prune Mar 27 '21 at 21:31
  • I don't quite understand your description of the problem. Could you show an example of: what `dfv` looks like before the merge; what `dfc` looks like before the merge; *exactly what the result should be for those inputs? – Karl Knechtel Mar 27 '21 at 21:38

1 Answers1

1

You can just say:

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

Also, btw, when you do pd.read_excel() it creates a dataframe, you don't need to then pass that into pd.DataFrame().

acrobat
  • 812
  • 4
  • 7