Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
Filtrado de un índice múltiple
C1 C2 C3 C4
A 12 Cierto 89
9 Falso 77
5 Cierto 23
B 9 Cierto 45
5 Cierto 45
2 Falso 78
C 11 Cierto 10
8 Falso 08
12 Falso 09

C1 y C2 son el índice múltiple. Espero obtener un resultado que me dé solo valores en C1 que tengan valores inferiores a 10 y mayores o iguales a 10 en C2.

Entonces, en la tabla anterior, C1 - B debería ir, con el resultado final debería verse así:

C1 C2 C3 C4
A 12 Cierto 89
9 Falso 77
5 Cierto 23
C 11 Cierto 10
8 Falso 08
12 Falso 09

Probé df.loc[(df.C2 < 10 ) & (df.C2 >= 10)] pero esto no funcionó.

También probé:

filtro1 = df.index.get_level_values('C2') < 10 filtro2 = df.index.get_level_values('C2') >= 10

df.iloc[filtro1 y filtro2]

Lo cual vi sugerido en otra publicación que tampoco funcionó. ¿Alguien sabe como resolver esto? Gracias

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use GroupBy.transform con GroupBy.any para probar al menos una coincidencia de condición por grupo, por lo que es posible el último filtro por m DataFrame:

 filter1 = df.index.get_level_values('C2') < 10 filter2 = df.index.get_level_values('C2') >= 10 m = (df.assign(filter1= filter1, filter2=filter2) .groupby(level=0)[['filter1','filter2']] .transform('any')) print (m) filter1 filter2 C1 C2 A 12 True True 9 True True 5 True True B 9 True False 5 True False 2 True False C 11 True True 8 True True 12 True True

 df = df[m.filter1 & m.filter2] print (df) C3 C4 C1 C2 A 12 True 89 9 False 77 5 True 23 C 11 True 10 8 False 8 12 False 9

Solución alternativa:

 filter1 = df.index.get_level_values('C2') < 10 filter2 = df.index.get_level_values('C2') >= 10 lvl1 = df.index[filter1].remove_unused_levels().levels[0] lvl2 = df.index[filter2].remove_unused_levels().levels[0] df1 = df.loc[set(lvl1).intersection(lvl2)] print (df1) C3 C4 C1 C2 A 12 True 89 9 False 77 5 True 23 C 11 True 10 8 False 8 12 False 9
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!