Thursday, December 17, 2015

Pandas chained indexing - example

Here we want to set all age > 2 values to NaN.    

import pandas as pd
import numpy as np
df = pd.DataFrame({'age':[1,2,3,4,5], 'income':[10,20,30,40,50]})
#guaranteed to work without warnings/errors  
df.loc[df.age > 2, 'age'] = np.NaN
print df
#won't work
df[df["age"] > 2]["age"] = np.NaN #error
#will work, but may give warning  
df["age"][df["age"] > 2] = np.NaN 
  

No comments:

Blog Archive