Loading [MathJax]/extensions/tex2jax.js

News

New paper!

Saturday, September 9, 2023

neat way to select rows in Pandas data frame

import pandas as pd
import numpy as np
df = pd.DataFrame({'a':range(10),'b':range(10,20),'c':np.zeros(10)}) # data
condition = np.where( (df.a > 5) & (df.b > 17) )[0] # return an array of row indices that match the condition
df.loc[df.index[condition],'c'] = 100 # Update the selected rows in column c