Creating rule function using pandas dataframe;

huangapple go评论105阅读模式
英文:

Creating rule function using pandas dataframe;

问题

我有一个以这种格式的数据框;

  1. rule_frame = pd.DataFrame({'PA_1' : ['A','B','A'],
  2. 'PA_2' : ['Low','Low','Low'],
  3. 'LA_1' : ['A','B','E'],
  4. 'LA_2' : ['Low','Low']})

我想创建一个函数,该函数以这个表格自动创建为输入。该函数基本上以每一行作为规则,如果符合规则,则返回true,否则返回false。

我所说的"自动创建函数"应该是这样的;

  1. def i_am_automatically_created(PA_1, PA_2, LA_1, LA_2):
  2. if PA_1 == 'A' and PA_2 == 'Low' and LA_1 == 'A' and LA_2 == 'Low':
  3. return True
  4. elif PA_1 == 'B' and PA_2 == 'Low' and LA_1 == 'B' and LA_2 == 'Low':
  5. return True
  6. elif PA_1 == 'A' and PA_2 == 'Low' and LA_1 == 'E' and LA_2 == 'High':
  7. return True
  8. else:
  9. return False

我之所以想要这样做的原因是列和行的大小可以不同。希望我能够解释清楚。

提前致谢。

英文:

I have a dataframe in this format;

  1. rule_frame = pd.DataFrame({'PA_1' : ['A','B','A'],
  2. 'PA_2' : ['Low','Low','Low'],
  3. 'LA_1' : ['A','B','E'],
  4. 'LA_2' : ['Low','Low']})

and I want to create a function that is automatically created by this table as an input. The function basically takes every single row as rules and return true, if not it should return false.

What i mean by "automatically created function" should be like;

  1. def i_am_automatically_created(PA_1,PA_2,LA_1,LA_2):
  2. if PA_1 == 'A' and PA_2 == 'Low' and LA_1 == 'A' and LA_2 == 'Low':
  3. return True
  4. elif PA_1 == 'B' and PA_2 == 'Low' and LA_1 == 'B' and LA_2 == 'Low':
  5. return True
  6. elif PA_1 == 'A' and PA_2 == 'Low' and LA_1 == 'E' and LA_2 == 'High':
  7. return True
  8. else:
  9. return False

The reason why I want to do this flexible is that column and row sizes can differ. I hope I was able to explain.

Thanks in advance.

答案1

得分: 1

你可以尝试:

  1. def create_func(df):
  2. all_funcs = []
  3. for _, r in df.iterrows():
  4. all_funcs.append(lambda *x, rules=r: all(r == v for v, r in zip(x, rules)))
  5. return lambda *x: any(f(*x) for f in all_funcs)
  6. fn = create_func(rule_frame)
  7. # 这应该通过:
  8. assert fn('A', 'Low', 'A', 'Low') == True
  9. assert fn('B', 'Low', 'B', 'Low') == True
  10. assert fn('A', 'Low', 'A', 'Low') == True
  11. assert fn('A', 'Low', 'E', 'High') == True
  12. # 这不应该通过:
  13. assert fn('A', 'High', 'E', 'High') == False
  14. assert fn('A', 'High', 'B', 'High') == False

初始 df:

  1. PA_1 PA_2 LA_1 LA_2
  2. 0 A Low A Low
  3. 1 B Low B Low
  4. 2 A Low E High
英文:

You can try:

  1. def create_func(df):
  2. all_funcs = []
  3. for _, r in df.iterrows():
  4. all_funcs.append(lambda *x, rules=r: all(r == v for v, r in zip(x, rules)))
  5. return lambda *x: any(f(*x) for f in all_funcs)
  6. fn = create_func(rule_frame)
  7. # this should pass:
  8. assert fn('A', 'Low', 'A', 'Low') == True
  9. assert fn('B', 'Low', 'B', 'Low') == True
  10. assert fn('A', 'Low', 'A', 'Low') == True
  11. assert fn('A', 'Low', 'E', 'High') == True
  12. # this shouldn't pass:
  13. assert fn('A', 'High', 'E', 'High') == False
  14. assert fn('A', 'High', 'B', 'High') == False

Initial df:

  1. PA_1 PA_2 LA_1 LA_2
  2. 0 A Low A Low
  3. 1 B Low B Low
  4. 2 A Low E High

huangapple
  • 本文由 发表于 2023年6月15日 02:00:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定