Apply loc for 2 columns values Pandas

January 24, 2018, at 7:08 PM

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)
Answer 1

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

paises_cpm = df.loc[a | b]


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)]
Rent Charter Buses Company
Numpy equivalent of math equation

Numpy equivalent of math equation

Transpose(p−q)M(p−q), with M as a semidefinite diagonal matrixWhat would the numpy equivalent be for a single result?

Past tweets using tweepy api

Past tweets using tweepy api

I'm using tweepy API in spyder to get past tweetsI'm using search method and passing parameters like query item, count, max_id and since_id but getting tweets not more than 1 week

Message: connection refused selenium python

Message: connection refused selenium python

I'm new to selenium and I'm trying to run firefox webdriver in virtual display