1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Slice column indexes in Python/pandas

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    Familiar with slicing in Python/pandas but how does this operate when a column-based index is set?

    Have a dataframe, d, that I've created a column-axis for as follows:

    levels_df = pd.DataFrame(levels)
    levels_df = levels_df.set_axis(new_labels,axis=1).iloc[2:]
    levels_df.head(10)
    Out[14]:
    Position 9.0 8.0 7.0 6.0 ... 7.0 8.0 9.0
    Side 1.0 1.0 1.0 1.0 ... 0.0 0.0 0.0 0.0
    2 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5
    3 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5
    4 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5
    5 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5
    6 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5
    7 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5
    8 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5
    9 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5
    10 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5
    11 4484.75 4485.0 4485.25 4485.5 ... 4488.75 4489.0 4489.25 4489.5

    [10 rows x 20 columns]


    levels_df

    Position 9.0 8.0 7.0 6.0 5.0 4.0 3.0 2.0 1.0 0.0 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
    Side 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
    2 4484.75 4485.0 4485.25 4485.5 4485.75 4486.0 4486.25 4486.5 4486.75 4487.0 4487.25 4487.5 4487.75 4488.0 4488.25 4488.5 4488.75 4489.0 4489.25 4489.5
    3 4484.75 4485.0 4485.25 4485.5 4485.75 4486.0 4486.25 4486.5 4486.75 4487.0 4487.25 4487.5 4487.75 4488.0 4488.25 4488.5 4488.75 4489.0 4489.25 4489.5
    4 4484.75 4485.0 4485.25 4485.5 4485.75 4486.0 4486.25 4486.5 4486.75 4487.0 4487.25 4487.5 4487.75 4488.0 4488.25 4488.5 4488.75 4489.0 4489.25 4489.5
    5 4484.75 4485.0 4485.25 4485.5 4485.75 4486.0 4486.25 4486.5 4486.75 4487.0 4487.25 4487.5 4487.75 4488.0 4488.25 4488.5 4488.75 4489.0 4489.25 4489.5
    6 4484.75 4485.0 4485.25 4485.5 4485.75 4486.0 4486.25 4486.5 4486.75 4487.0 4487.25 4487.5 4487.75 4488.0 4488.25 4488.5 4488.75 4489.0 4489.25 4489.5
    7 4484.75 4485.0 4485.25 4485.5 4485.75 4486.0 4486.25 4486.5 4486.75 4487.0 4487.25 4487.5 4487.75 4488.0 4488.25 4488.5 4488.75 4489.0 4489.25 4489.5
    8 4484.75 4485.0 4485.25 4485.5 4485.75 4486.0 4486.25 4486.5 4486.75 4487.0 4487.25 4487.5 4487.75 4488.0 4488.25 4488.5 4488.75 4489.0 4489.25 4489.5


    How do I slice such a dataframe to retrieve a filtered version of the df?

    For instance, something like this:

    levels_df.iloc[:5,(levels_df.Position==8) & (levels_df.Side==1)]


    Would return:

    Position 8.0
    Side 1.0
    2 4485.0
    3 4485.0
    4 4485.0
    5 4485.0
    6 4485.0


    Instead of the error:

    levels_df[(levels_df.Position==8) & (levels_df.Side==1)]
    Traceback (most recent call last):

    File "/tmp/ipykernel_424060/105426524.py", line 1, in <module>
    levels_df[(levels_df.Position==8) & (levels_df.Side==1)]

    File "/home/chris/anaconda3/lib/python3.9/site-packages/pandas/core/generic.py", line 5575, in __getattr__
    return object.__getattribute__(self, name)

    AttributeError: 'DataFrame' object has no attribute 'Position'

    Continue reading...

Compartilhe esta Página