使用pandas的.loc出现意外结果 – 尝试根据条件连接2个列

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

Unexpected results using pandas .loc - trying to concatenate 2 columns based on a condition

问题

以下是您要的代码部分的中文翻译:

  1. # 我有一个数据框,我正在尝试根据条件合并两列。
  2. # 创建数据框
  3. df = em_df[['Redcap_Case_num', 'EV_EM', 'COMP_EM', 'EV_RND', 'COMP_EM_RND']].head(3)
  4. df.to_clipboard(excel=False, sep=', ')
  5. # 将EMFREAS - RND列添加到数据框
  6. cols_to_fill = [x for x in ln.columns if x.startswith("EMFREAS")]
  7. for column in cols_to_fill:
  8. df[column] = ln[column].copy()
  9. df.to_clipboard(excel=False, sep=', ')
  10. # 请注意,我尝试按照格式化表格的说明操作,但说明对我无效。我不确定我做错了什么,所以这是我能够完成的方式。

以下是您的期望输出的中文翻译:

  1. # 预期的输出应如下所示:
  2. # 需要合并EV_RND列和COMP_EM_RND列,以填充所有以EMFREAS开头的列(您只看到了部分列)。
  3. EV_ND = df["EV_EM"] == 0
  4. EM_ND = df['COMP_EM'] == 'Not Done'
  5. df.loc[EV_ND | EM_ND, cols_to_fill] = df["EV_RND"] + '|' + df["COMP_EM_RND"]
  6. # 预期的结果应如下所示:
  7. # 需要合并EV_RND列和COMP_EM_RND列,以填充所有以EMFREAS开头的列(您只看到了部分列)。
  8. ID EV_EM COMP_EM EV_RND COMP_EM_RND EMFREAS1 EMFREAS2
  9. YA007 1 Not Done EV ND Insufficient Insufficient|EV ND Insufficient|EV ND
  10. YA006 1
  11. YA005 1 Outside grid EM Not done EM Not done EM Not done

以上是代码和期望输出的中文翻译,不包含其他内容。

英文:

I have a df where I am trying to merge 2 columns based on a condition.

Create df

  1. df = em_df[['Redcap_Case_num', 'EV_EM', 'COMP_EM', 'EV_RND', 'COMP_EM_RND'] ].head(3)
  2. df.to_clipboard(excel = False, sep = ', ')
  3. #Add EMFREAS - RND columns to df
  4. cols_to_fill=[x for x in ln.columns if x.startswith("EMFREAS")]
  5. for column in cols_to_fill:
  6. df[column] = ln[column].copy()
  7. df.to_clipboard(excel = False, sep = ', ')

Output - Please understand I have tried to follow the instructions to format the table, but the instructions did not work for me. I'm not sure what I am doing wrong so this is how I was able to do it.

  1. ID EV_EM COMP_EM EV_RND COMP_EM_RND EMFREAS1 EMFREAS2
  2. YA007 1 Not Done Insufficient
  3. YA006 1
  4. YA005 0 Outside grid EM Not done

I need to merge the EV_RND column and the COMP_EM_RND columns to populate all the columns that start with EMFREAS (You are only seeing a subset of the columns)

Here is the code I am trying to use to do this:

  1. #apply ND filter to df and merge to ln df
  2. EV_ND = df["EV_EM"]==0
  3. EM_ND = df['COMP_EM'] == 'Not Done'
  4. df.loc[EV_ND | EM_ND, cols_to_fill]=df["EV_RND"] + '|' + df["COMP_EM_RND"]

The expected outcome should look like this:

  1. ID EV_EM COMP_EM EV_RND COMP_EM_RND EMFREAS1 EMFREAS2
  2. YA007 1 Not Done EV ND Insufficient Insufficient|EV ND Insufficient|EV ND
  3. YA006 1
  4. YA005 1 Outside grid EM Not done EM Not done EM Not done

答案1

得分: 1

如果您的数据框中的空值实际上是空字符串,您可以创建一个分隔符系列,如果 EV_RNDCOMP_EM_RND 不为空,那么该系列将等于 |,否则为空字符串。然后连接 EV_RND,分隔符系列和 COMP_EM_RND

  1. sep_series = df.apply(lambda x: '|'
  2. if (x['EV_RND'] and x['COMP_EM_RND'])
  3. else '', axis=1)
  4. fill_series = df['EV_RND'].str.cat(sep_series).str.cat(df['COMP_EM_RND'])
  5. for col in df.columns:
  6. if col.startswith('EMFREAS'):
  7. df[col] = df[col].replace('', np.nan).fillna(fill_series)

输出:

  1. ID EV_EM COMP_EM EV_RND COMP_EM_RND EMFREAS1 EMFREAS2
  2. 0 YA007 1 Not Done EV ND Insufficient EV ND|Insufficient EV ND|Insufficient
  3. 1 YA006 1 EV ND|Insufficient
  4. 2 YA005 0 Outside grid EM Not done EM Not done EM Not done
英文:

If the empty values in your df are actually empty strings, you can create a separator series equal to | if EV_RND and COMP_EM_RND are not empty, empty string otherwise. Then concat EV_RND, the separator series and COMP_EM_RND:

  1. sep_series = df.apply(lambda x: '|'
  2. if (x['EV_RND'] and x['COMP_EM_RND'])
  3. else '', axis=1)
  4. fill_series = df['EV_RND'].str.cat(sep_series).str.cat(df['COMP_EM_RND'])
  5. for col in df.columns:
  6. if col.startswith('EMFREAS'):
  7. df[col] = df[col].replace('', np.nan).fillna(fill_series)

Output:

  1. ID EV_EM COMP_EM EV_RND COMP_EM_RND EMFREAS1 EMFREAS2
  2. 0 YA007 1 Not Done EV ND Insufficient EV ND|Insufficient EV ND|Insufficient
  3. 1 YA006 1
  4. 2 YA005 0 Outside grid EM Not done EM Not done EM Not done

huangapple
  • 本文由 发表于 2023年2月27日 01:47:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573902.html
匿名

发表评论

匿名网友

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

确定