Apply loc for 2 columns values Pandas

≡放荡痞女 提交于 2021-01-27 14:50:41

问题


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)

回答1:


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)
                   ]


来源:https://stackoverflow.com/questions/48423462/apply-loc-for-2-columns-values-pandas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!