pandas - Python perform operation in string -
so i'm trying pass variable operation (user defined) function , having trouble trying find way of doing it. can think of hard code options function following:
def dothings(conditions): import re import pandas pd d = {'time' : pd.series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd']), 'legnth' : pd.series([4., 5., 6., 7.], index=['a', 'b', 'c', 'd'])} df = pd.dataframe(d) print df condition in conditions: # split condition 2 parts splitcondition = re.split('<=|>=|!=|<|>|=',condition) # if right side of conditional statement number convert float if splitcondition[1].isdigit(): splitcondition[1] = float(splitcondition[1]) # perform condition specified if "<=" in condition: df = df[df[splitcondition[0]]<=splitcondition[1]] print "one" elif ">=" in condition: df = df[df[splitcondition[0]]>=splitcondition[1]] print "two" elif "!=" in condition: df = df[df[splitcondition[0]]!=splitcondition[1]] print "three" elif "<" in condition: df = df[df[splitcondition[0]]<=splitcondition[1]] print "four" elif ">" in condition: df = df[df[splitcondition[0]]>=splitcondition[1]] print "five" elif "=" in condition: df = df[df[splitcondition[0]]==splitcondition[1]] print "six" return df # specify conditions conditions = ["time>2","legnth<=6"] df = dothings(conditions) # call function print df
which results in this:
legnth time 4 1 b 5 2 c 6 3 d 7 4 5 1 legnth time c 6 3
this , , everything, i'm wondering if there better or more efficient way of passing conditions functions without writing if statements possible out. ideas?
solution:
def dothings(conditions): import re import pandas pd d = {'time' : pd.series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd']), 'legnth' : pd.series([4., 5., 6., 7.], index=['a', 'b', 'c', 'd'])} df = pd.dataframe(d) print df condition in conditions: # split condition 2 parts splitcondition = re.split('<=|>=|!=|<|>|=',condition) # if right side of conditional statement number convert float if splitcondition[1].isdigit(): splitcondition[1] = float(splitcondition[1]) import operator ops = {'<=': operator.le, '>=': operator.ge, '!=': operator.ne, '<': operator.lt, '>': operator.gt, '=': operator.eq} cond = re.findall(r'<=|>=|!=|<|>|=', condition) df = df[ops[cond[0]](df[splitcondition[0]],splitcondition[1])] return df # specify conditions conditions = ["time>2","legnth<=6"] df = dothings(conditions) # call function print df
output:
legnth time 4 1 b 5 2 c 6 3 d 7 4 legnth time c 6 3
you can access built-in operators via operator
module, , build table mapping operator names built-in ones, in cut-down example:
import operator ops = {'<=': operator.le, '>=': operator.ge} in [3]: ops['>='](2, 1) out[3]: true
Comments
Post a Comment