为 2 列值应用 loc Pandas
Apply loc for 2 columns values Pandas
我想定位一个包含 2 列参数的数据框:
如果我这样做 paises_cpm = df.loc[a]
正在工作,但如果我这样做 paises_cpm = df.loc[a,b]
我收到错误消息: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)
您需要 |
用于 or
或 &
用于 and
的链条件:
paises_cpm = df.loc[a | b]
或:
paises_cpm = df.loc[a & b]
有一种可能的解决方案,但括号是必要的:
paises_cpm = df.loc[(df['Subastas'] > subastas) |
(df['Impresiones_exchange'] > impresiones)
]
我想定位一个包含 2 列参数的数据框:
如果我这样做 paises_cpm = df.loc[a]
正在工作,但如果我这样做 paises_cpm = df.loc[a,b]
我收到错误消息: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)
您需要 |
用于 or
或 &
用于 and
的链条件:
paises_cpm = df.loc[a | b]
或:
paises_cpm = df.loc[a & b]
有一种可能的解决方案,但括号是必要的:
paises_cpm = df.loc[(df['Subastas'] > subastas) |
(df['Impresiones_exchange'] > impresiones)
]