您好,我有两个数据框。
输入数据帧:-
id number idsc mfd
738 as6812 *fage abc van brw amz
745 786-151 *glaeceau smt sp amz
759 b0nadum ankush 574415 mo... admz
764 fdad3-al-c lib anvest-al... amz
887 rec-2s-5 or abc sur... c
64 00954 ankush pure g... amz
8 0000686 dor must die a
3 000adf623 bsc test 10-pi... amz
检查条件数据框:-
condition destinationfield expression b_id
True idsc [idsc].lower() 1
[mfd]=="amz" idsc re.sub(r'\abc\b','a',[idsc]) 1
[mfd]=="admz" idsc re.sub(r'and \d+ other item', '', [idsc]) 1
True idsc re.sub(r'[^a-z0-9 ]',r'',[idsc]) 1
True idsc [idsc].strip() 1
[mfd] == "c" idsc re.sub(r'\ankush\b','ank',[idsc]) 1
True number re.sub(r'[^0-9]',r'',[number]) 1
True number [number].strip() 1
我正在寻找在输入数据帧上应用条件数据帧的每条规则并获得一个新的数据帧。
如果我的条件为真,那么我需要将它应用于所有行。如果除了 true 之外还有任何特定值,那么我需要将该条件应用于特定记录。
在 pyspark 中是否有更好的方法来执行此操作,因为正则表达式与 python 相关。而不是在 for 循环中运行它。
id number idsc mfd
738 as6812 *fage a van brw amz
745 786-151 *glaeceau smt sp amz
759 b0nadum ank 574415 mo... admz
764 fdad3-al-c lib anvest-al... amz
887 rec-2s-5 or a sur... c
64 00954 ank pure g... amz
8 0000686 dor must die a
3 000adf623 bsc test 10-pi... amz
输入数据管道分离
id| number|idsc|mfd
738|as6812|*fage abc van brw|amz
745|786-151|*glaeceau smt sp|amz
759|b0nadum|ankush 574415 mo...|admz
764|fdad3-al-c|lib anvest-al...|amz
887|rec-2s-5|or abc sur...|c
64| 00954|ankush pure g...|amz
8|0000686|dor must die a
3|000adf623|bsc test 10-pi...|amz
条件数据管道分离
条件|目标字段|表达式|b_id|
True|idsc|[idsc].lower()|1
[mfd]=="amz"|idsc|re.sub(r'\abc\b','a',[idsc])|1
[mfd]=="admz"|idsc|re.sub(r'and \d+ other item', '', [idsc])|1
True|idsc|re.sub(r'[^a-z0-9 ]',r'',[idsc])|1
True|idsc|[idsc].strip()|1
[mfd] == "c"|idsc|re.sub(r'\ankush\b','ank',[idsc])|1
True|number|re.sub(r'[^0-9]',r'',[number])|1
True|number|[number].strip()|1
谢谢, 安库什雷迪
最佳答案
您可以尝试使您的条件数据框可评估。
如果它是可评估的,您可以在条件和表达式上调用 eval()
。
def apply_condition(df, df_condition):
# write a function get_df_evaluable_condition which both does
# replace "[any_column]" by "df.['any_column']" in createcondition
# replace "[destinationfield]" by "element" in expression
df_evaluable_condition = get_df_evaluable_condition(df_evaluable_condition)
for index, row in df_evaluable_condition.iterrows():
createcondition = row['createcondition']
destinationfield = row['destinationfield']
expression = row['expression']
# just apply expression where createcondition is true
df.loc[eval(createcondition), destinationfield] =
df.loc[eval(createcondition), destinationfield].apply(lambda element: eval(expression))
顺便说一句,如果表达式包含对不是目标列的列的引用,最后一行代码将不起作用。您将需要更复杂的东西来实现您想要的。
如果您不知道您的 Condition Dataframe 是什么样子,我不建议您使用此方法。不要对未知字符串调用 eval()
!
关于python - 通过应用来自第二个数据框的规则来改变数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50932126/