4

I´m tying to loc a dataframe with 2 columns parameters: if I do paises_cpm = df.loc[a]is working but if I do paises_cpm = df.loc[a,b] I receive an error: IndexingError: Unalignable boolean Series provided as indexer (index of the boolean Series and of the indexed object do not match

import pandas as pd
import time


fecha = time.strftime(str((int(time.strftime("%d")))-1))

subastas = int(fecha) * 5000
impresiones = int(fecha) * 1000

df = pd.read_csv('Cliente_x_Pais.csv')
a = df['Subastas'] > subastas
b = df['Impresiones_exchange'] > impresiones


paises_cpm = df.loc[a,b]

paises_cpm.to_csv('paises_cpm.csv', index=False)
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
Martin Bouhier
  • 361
  • 2
  • 6
  • 19

1 Answers1

7

You need chain conditions with | for or or & for and:

paises_cpm = df.loc[a | b]

Or:

paises_cpm = df.loc[a & b]

There is possible one line solution, but parentheses are necessary:

paises_cpm = df.loc[(df['Subastas'] > subastas) | 
                    (df['Impresiones_exchange'] > impresiones)
                   ]
yoonghm
  • 4,198
  • 1
  • 32
  • 48
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Can you tell my how can I sort by values with that: `paises_cpm = paises_cpm.sort_values(by=['Country', 'Subastas'],ascending=False)` It works but I need to do it with `Country ascending and Subastas descending` – Martin Bouhier Jan 24 '18 at 13:33
  • 1
    You are really close, need change `ascending=False` to `ascending=[True, False]` – jezrael Jan 24 '18 at 13:34