【问题标题】:I wish to replicate conditional column from Power Query M function in Python using Pandas我希望使用 Pandas 从 Python 中的 Power Query M 函数中复制条件列
【发布时间】:2021-06-09 23:37:59
【问题描述】:

我一直在使用 Power Query 处理一些数据,框架的灵活性给我留下了深刻的印象。目前,我希望在 Pandas 中复制条件列步骤,因为我想将其包含在自动数据清理脚本管道中。

在这种情况下,Power Query 创建一个名为 acc_col 的新列,查看数据集中的每一列(Tags.1、Tags.2 等)以及该列中的字符串是否与值的开头匹配(Acceleration- ) 然后将该值输出到新列中,否则如果未找到匹配项,则输出 Unknown Acc。 这是编辑器的样子

 #"Added Conditional Column" = Table.AddColumn(#"Replaced Value", "acc_col", each if Text.StartsWith([Tags.1], "Acceleration-") then [Tags.1] else if Text.StartsWith([Tags.2], "Acceleration-") then [Tags.2] else if Text.StartsWith([Tags.3], "Acceleration-") then [Tags.3] else if Text.StartsWith([Tags.4], "Acceleration-") then [Tags.4] else if Text.StartsWith([Tags.5], "Acceleration-") then [Tags.5] else "Unknown Acc")

我在 Pandas 上尝试了一些东西,但我的知识有点有限。我设法使用以下方法读取了标签列之一

Tags0 Tags1 Tags2
Alumni-2017,Acceleration-2016 None None
Alumni Acceleration-2017 None
Acceleration-2015 None None
Alumni-2017 Acceleration-2015 None
Alumni-2017 Acceleration-2014 None
df['acc_col'] = df['Tags0'].where(df['Tags0'].str.contains('Acceleration', na=False), )
Tags0 Tags1 Tags2 acc_col
Alumni-2017,Acceleration-2016 None None Acceleration-2016
Alumni Acceleration-2017 None None
Acceleration-2015 None None Acceleration-2015
Alumni-2017 Acceleration-2015 None None
Alumni-2017 Acceleration-2014 None None

我看到输出包含所有包含关键字的内容,但如果我希望对其他列执行相同操作,它会覆盖以前的结果。我需要它们都在同一列上,因为它一一阅读。

 df['acc_col'] = df['Tags1'].where(df['Tags1'].str.contains('Acceleration', na=False), )
Tags0 Tags1 Tags2 acc_col
Alumni-2017,Acceleration-2016 None None None
Alumni Acceleration-2017 None Acceleration-2017
Acceleration-2015 None None None
Alumni-2017 Acceleration-2015 None Acceleration-2015
Alumni-2017 Acceleration-2014 None Acceleration-2014

我觉得我很接近了,但我需要更多帮助。

【问题讨论】:

  • 请添加示例数据框,并提供预期输出。请添加数据,而不是图片或重定向
  • @sammywemmy 嘿,我编辑了外观,以便我包含数据框。我想添加图片作为我在 powerBI 中所做工作的参考

标签: python pandas powerbi powerquery


【解决方案1】:

我想我设法回答了我自己的问题。我只需要将条件的结果添加到另一个 pd.where 语句并继续,直到扫描完所有列。

df['Acceleration'] = df['Tags0'].where(df['Tags0'].str.contains('Acceleration', na=False), 
                                                       df['Tags1'].where(df['Tags1'].str.contains('Acceleration', na=False),
                                                       df['Tags2'].where(df['Tags2'].str.contains('Acceleration', na=False),
                                                       df['Tags3'].where(df['Tags3'].str.contains('Acceleration', na=False),
                                                       df['Tags4'].where(df['Tags4'].str.contains('Acceleration', na=False),'Unknown')))))

【讨论】:

    【解决方案2】:

    试试这个,看看是不是你想要的:

    transform

    这里假设所有列都是字符串;如果没有,您可以先选择 dtypes 进行一些修改:

    condition = df.transform(lambda x: x.str.contains("Acceleration", na=False))
    

    在整个数据框上使用where 函数创建新列,并在columns 轴上使用forward filling 以获得所需的列:

    df.assign(acc_col=df.where(condition).ffill(axis = 'columns').iloc[:, -1])
    
                               Tags0              Tags1 Tags2                        acc_col
    0  Alumni-2017,Acceleration-2016               None  None  Alumni-2017,Acceleration-2016
    1                         Alumni  Acceleration-2017  None              Acceleration-2017
    2              Acceleration-2015               None  None              Acceleration-2015
    3                    Alumni-2017  Acceleration-2015  None              Acceleration-2015
    4                    Alumni-2017  Acceleration-2014  None              Acceleration-2014
    

    如果acc_col 中有任何空值,您可以使用replace 函数或fillna 将其填充为您的首选值(“未知”)。

    在 Power Query 中,我认为您可以将列转换为列表,并在列表中运行搜索/提取,(可能使用 List.Contains)结合单个 if 语句,而不是单独的 if/else 检查.这可能需要您编写 M 代码。我的能量查询技能有一段时间没有被调用,所以我的建议可能是错误的。

    【讨论】:

      猜你喜欢
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多