how to select the data of a Dataframe (Python)

  • ahoi,

    i have gotta admit i struggle a bit with the querying of data from hierarchical indexed dataframes.

    Heres the dataframe creation.

    import pandas as pd
    import numpy as np

    multi_indexed_DF = pd.DataFrame(np.random.rand(4, 2),
    index=[['a', 'a', 'b', 'b'], [1, 2, 1, 2]],
    columns=['data1', 'data2'],)

    How do i comebine the first 2 selection methods (filter_1, filter_2) to get a propery combination of both filters:

    # get only columns data1,data2 from first index with 'a', 'b' and for each one only the 2nd index val=1
    filter_1 = multi_indexed_DF.loc[(['a','b'], 1), ['data1','data2']]

    # get only those entries where data1 is more than 0.5 (random numbers so the result varies)
    filter_2 = multi_indexed_DF.loc[multi_indexed_DF.data1 > 0.5]

    # so my question is how do i add the 2nd filter of data1 > 0.5 on on top of the first selection
    # the full dataframe multi_indexed_DF, filter_1, filter_2
    # this one seems to work, but the system tells me not to use it because its depricated and will be gone
    # so i am looking for the "proper" way(s) toactually write this
    filter_3 = multi_indexed_DF[multi_indexed_DF.data1 > 0.5].loc[(['a','b'], 1), ['data1','data2']]

    """
    # here is the result example from an execution with random numbers of mine for
    # FULL
    data1 data2
    a 1 0.295029 0.815447
    2 0.481834 0.529683
    b 1 0.981762 0.021112
    2 0.995068 0.579253

    # Filter 1
    data1 data2
    a 1 0.295029 0.815447
    b 1 0.981762 0.021112

    # Filter 2
    data1 data2
    b 1 0.981762 0.021112
    2 0.995068 0.579253

    # Execpted Result for combined Filter 3: result of filter 1 without index a 1 because a data1 > 0.5
    data1 data2
    b 1 0.981762 0.021112
    """

    Thanks

     

     

  • Thanks for posting your issue and hopefully someone will answer soon.

    This is an automated bump to increase visibility of your question.

  • This was removed by the editor as SPAM

  • This was removed by the editor as SPAM

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply