Filtering a Pythons Pandas DataFrame based on values from dictionary

CiaranWelsh

I have some data that I have read into Python as a pandas dataframe:

             Unnamed: 0  Initial_guess  Lower_bound  Upper_bound Estimated_or_Fixed  
      0          Ka              5     0.000001        10000          Estimated   
      2          Kd              5     0.000001        10000          Estimated   
      3          Ki              5     0.000001        10000          Estimated   
      5          Kr              5     0.000001        10000          Estimated   
      6        R1_I              5     0.000001        10000          Estimated   
      7         PR1              5     0.000001        10000          Estimated   
      8         PR2              5     0.000001        10000          Estimated   
      9       alpha              5     0.000001        10000          Estimated   
      10        Kcd              5     0.000001        10000          Estimated   
      12       Klid              5     0.000001        10000          Estimated   
      18    LR1R2_I              5     1.000000        10000          Estimated   

        Variable_type  
0   Kinetic parameter  
2   Kinetic parameter  
3   Kinetic parameter  
5   Kinetic parameter  
6   Kinetic parameter  
7   Kinetic parameter  
8   Kinetic parameter  
9   Kinetic parameter  
10  Kinetic parameter  
12  Kinetic parameter  
18         Species IC  

The first column unnamed: 0 are parameters. I have many models each containing different combinations of these parameters. My task is to filter this table for each model by removing any row who's parameter is not present in the model. I have dictionaries for each model with the parameters they contain. Parameters can be of two types, species IC or kinetic parameter. Here is an example of these dictionaries for the first model:

Species_IC:
{'R1': '2.7109e+02', 'R2': '1.2709e+02', 'R1_I': '2.7109e+03', 'R2_I': '1.2709e+03', 'LR1R2': '1.6913e+00', 'LR1R2_I': '1.6913e+01'}

Kinetic_parameter:
{'Ka': '1.0000e+00', 'TGFb': '1.0000e-01', 'Synth': '1.0000e+00', 'PR1': '8.0000e+00', 'Sink': '0.0000e+00', 'PR2': '4.0000e+00', 'alpha': '1.0000e+00'}

My Code:

def write_parameter_bounds_file(self):
    model1=self.all_models_dirs[0] #get first model from a list of model. I'll do it on the first model then generalize to the rest. 
    species=self.get_model_species(model1+'.xml') #get the species dct from this model
    parameters=self.get_model_parameters(model1+'.xml')#get parameter dct from this model
    param_info=self.read_parameter_bounds_template() #get all parameters from template. This is the pandas dataframe at the top. 
    estimated_species=[]
    estimated_params=[]
    for i in species.keys():
        print '\n'
        for j in param_info[param_info.columns[0]]:
            if i==j:
                estimated_species.append(i)
    for i in parameters.keys():
        print '\n'
        for j in param_info[param_info.columns[0]]:
            if i==j:
                estimated_params.append(i)
    param_list=estimated_params+estimated_species #This is a list of the parameters that need to be included in the output df

Does anybody know how I can use param_list to filter the original pandas df?

Thanks

jezrael

You can use function isin with your list generated from dictionary:

list_Species_IC = Species_IC.keys()

and get subset of dataframe df. You can reset index by function reset_index.

Similar approach can be use for dictionaryKinetic_parameter.

Species_IC = {'R1': '2.7109e+02', 'R2': '1.2709e+02', 'R1_I': '2.7109e+03', 'R2_I': '1.2709e+03', 'LR1R2': '1.6913e+00', 'LR1R2_I': '1.6913e+01'}

list_Species_IC = Species_IC.keys()
print list_Species_IC
#['R1', 'R2', 'R1_I', 'R2_I', 'LR1R2', 'LR1R2_I']
out = df[df['Unnamed: 0'].isin(list_Species_IC)].reset_index()
print out
#   Unnamed: 0  Initial_guess  Lower_bound  Upper_bound Estimated_or_Fixed
#4        R1_I              5     0.000001        10000          Estimated
#10    LR1R2_I              5     1.000000        10000          Estimated

All together:

Species_IC = {'R1': '2.7109e+02', 'R2': '1.2709e+02', 'R1_I': '2.7109e+03', 'R2_I': '1.2709e+03', 'LR1R2': '1.6913e+00', 'LR1R2_I': '1.6913e+01'}
Kinetic_parameter = {'Ka': '1.0000e+00', 'TGFb': '1.0000e-01', 'Synth': '1.0000e+00', 'PR1': '8.0000e+00', 'Sink': '0.0000e+00', 'PR2': '4.0000e+00', 'alpha': '1.0000e+00'}

list_Species_IC = Species_IC.keys()
list_Kinetic_parameter = Kinetic_parameter.keys()
list_IC = list_Species_IC + list_Kinetic_parameter
print list_IC
#['R1', 'R2', 'R1_I', 'R2_I', 'LR1R2', 'LR1R2_I', 'Ka', 'TGFb', 'Synth', 'PR1', 'Sink', 'PR2', 'alpha']
out = df[df['Unnamed: 0'].isin(list_IC)].reset_index()
print out
#   index Unnamed: 0  Initial_guess  Lower_bound  Upper_bound  \
#0      0         Ka              5     0.000001        10000   
#1      4       R1_I              5     0.000001        10000   
#2      5        PR1              5     0.000001        10000   
#3      6        PR2              5     0.000001        10000   
#4      7      alpha              5     0.000001        10000   
#5     10    LR1R2_I              5     1.000000        10000   
#
#  Estimated_or_Fixed  
#0          Estimated  
#1          Estimated  
#2          Estimated  
#3          Estimated  
#4          Estimated  
#5          Estimated  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Filtering a Pythons Pandas DataFrame based on values from dictionary

From Dev

How to increment Python Pandas DataFrame based on key/values from a dictionary

From Dev

Creating Dictionary from Pandas DataFrame Column Based on Unique Values in Column

From Dev

Pandas filtering values in dataframe

From Dev

Pandas filtering values in dataframe

From Dev

Extracting values as a dictionary from dataframe based on list

From Dev

Filtering rows from dataframe based on the values of the previous rows

From Dev

Filtering duplicates from pandas dataframe with preference based on additional column

From Dev

Pandas: Change dataframe values based on dictionary and remove rows with no match

From Dev

Filtering a dataframe using a dictionary's values

From Dev

Dictionary from Pandas dataframe

From Dev

Construct Pandas DataFrame from dictionary in form {index: list of row values}

From Dev

New pandas dataframe column using values from python dictionary

From Dev

Assign values to columns in Pandas Dataframe based on data from another dataframe

From Dev

Filtering nil values from a dictionary created with Mirror

From Dev

Overwriting values in a pandas dataframe based on NA values from a second one

From Dev

Pandas DataFrame filtering based on second column

From Dev

Pandas DataFrame filtering based on second column

From Dev

Pandas dataframe filtering based on group counts

From Dev

Selecting rows from a Dataframe based on values in multiple columns in pandas

From Dev

Deleting rows from Pandas dataframe based on groupby values

From Dev

Select CONSECUTIVE rows from a DataFrame based on values in a column in Pandas with Groupby

From Dev

Select rows from a DataFrame based on multiple values in a column in pandas

From Dev

Select rows from a DataFrame based on last characters of values in a column in pandas

From Dev

Plotting data from a Pandas Dataframe with distinct curves based on column values

From Dev

Creating a pandas dataframe from a dictionary

From Dev

Pandas dataframe from nested dictionary

From Dev

Pandas map to DataFrame from dictionary

From Dev

Pandas DataFrame from Dictionary with Lists

Related Related

  1. 1

    Filtering a Pythons Pandas DataFrame based on values from dictionary

  2. 2

    How to increment Python Pandas DataFrame based on key/values from a dictionary

  3. 3

    Creating Dictionary from Pandas DataFrame Column Based on Unique Values in Column

  4. 4

    Pandas filtering values in dataframe

  5. 5

    Pandas filtering values in dataframe

  6. 6

    Extracting values as a dictionary from dataframe based on list

  7. 7

    Filtering rows from dataframe based on the values of the previous rows

  8. 8

    Filtering duplicates from pandas dataframe with preference based on additional column

  9. 9

    Pandas: Change dataframe values based on dictionary and remove rows with no match

  10. 10

    Filtering a dataframe using a dictionary's values

  11. 11

    Dictionary from Pandas dataframe

  12. 12

    Construct Pandas DataFrame from dictionary in form {index: list of row values}

  13. 13

    New pandas dataframe column using values from python dictionary

  14. 14

    Assign values to columns in Pandas Dataframe based on data from another dataframe

  15. 15

    Filtering nil values from a dictionary created with Mirror

  16. 16

    Overwriting values in a pandas dataframe based on NA values from a second one

  17. 17

    Pandas DataFrame filtering based on second column

  18. 18

    Pandas DataFrame filtering based on second column

  19. 19

    Pandas dataframe filtering based on group counts

  20. 20

    Selecting rows from a Dataframe based on values in multiple columns in pandas

  21. 21

    Deleting rows from Pandas dataframe based on groupby values

  22. 22

    Select CONSECUTIVE rows from a DataFrame based on values in a column in Pandas with Groupby

  23. 23

    Select rows from a DataFrame based on multiple values in a column in pandas

  24. 24

    Select rows from a DataFrame based on last characters of values in a column in pandas

  25. 25

    Plotting data from a Pandas Dataframe with distinct curves based on column values

  26. 26

    Creating a pandas dataframe from a dictionary

  27. 27

    Pandas dataframe from nested dictionary

  28. 28

    Pandas map to DataFrame from dictionary

  29. 29

    Pandas DataFrame from Dictionary with Lists

HotTag

Archive