What I am looking to do is to put the rules of slicing a pandas dataframe in a function.
For example:
row1 = {'a':5,'b':6,'c':7,'d':'A'}
row2 = {'a':8,'b':9,'c':10,'d':'B'}
row3 = {'a':11,'b':12,'c':13,'d':'C'}
df = pd.DataFrame([row1,row2,row3])
I am slicing the dataframe this way:
print df.loc[df['a']==5]
print df.loc[df['b']==12]
print df.loc[(df['b']==12) | df['d'].isin(['A','C']),'d']
For my purposes, I need to slice the same dataframe in different ways as part of a function. For example:
def slicing(locationargument):
df.loc(locationargument)
do some stuff..
return something
Alternatively, I was expecting getattr() to work but that tells me DataFrames do not have a .loc[...] attribute. For example:
getattr(df,"loc[df['a']==5]")
Returns:
AttributeError: 'DataFrame' object has no attribute 'loc[df['a']==5]'
Am I missing something here? Any thoughts or alternatives would be greatly appreciated!