基于多条件逻辑从单独的 Pandas 数据帧返回字符串片段

商业银行

我是 python 的新手,并试图用 Pandas 中的数据帧做一些工作

左侧是主数据帧 (df1) 的一部分,右侧是第二个 (df2)。目标是使用基于几个条件逻辑的字符串填充 df1['vd_type'] 列。我可以使用嵌套的 np.where() 函数来完成这项工作,但是随着它深入层次结构,运行时间太长了,所以我正在寻找一个更优雅的解决方案。

逻辑的英文版本是这样的:对于 df1['vd_type']:如果 df1['shape'] == df2['vd_combo'] 中的前两个字符 AND df1['vd_pct'] <= df2['combo_value '],然后在这两个条件都为真的行上返回 df2['vd_combo'] 中的最后 3 个字符。如果在 df2 中找不到两个条件都为真的行,则返回“vd4”。

提前致谢!


编辑#2:所以我想基于另一个变量实现第三个条件,其他一切都相同,除了在 df1 中还有另一列“log_vsc”具有现有值,目标是填写一个空的 df1 列“vsc_type” ' 与同一方案中的 4 个字符串之一。额外的条件只是我们刚刚定义的“vd_type”将与拆分“vsc_combo”产生的“vd”列匹配。

df3 = pd.DataFrame()
df3['vsc_combo'] = ['A1_vd1_vsc1','A1_vd1_vsc2','A1_vd1_vsc3','A1_vd2_vsc1','A1_vd2_vsc2' etc etc etc
df3['combo_value'] = [(number), (number), (number), (number), (number), etc etc

df3[['shape','vd','vsc']] = df3['vsc_combo'].str.split('_', expand = True)

def vsc_condition( row, df3):
    df_select = df3[(df3['shape'] == row['shape']) & (df3['vd'] == row['vd_type']) & (row['log_vsc'] <= df3['combo_value'])]
    if df_select.empty:
        return 'vsc4'
    else:
        return df_select['vsc'].iloc[0]

## apply vsc_type
df1['vsc_type'] = df1.apply( vsc_condition, args = ([df3]), axis = 1)

这有效!!再次感谢!

我想要一片T骨牛排

所以你的输入是这样的:

import pandas as pd
df1 = pd.DataFrame({'shape': ['A2', 'A1', 'B1', 'B1', 'A2'],
                    'vd_pct': [0.78, 0.33, 0.48, 0.38, 0.59]} )
df2 = pd.DataFrame({'vd_combo': ['A1_vd1', 'A1_vd2', 'A1_vd3', 'A2_vd1', 'A2_vd2', 'A2_vd3', 'B1_vd1', 'B1_vd2', 'B1_vd3'],
                    'combo_value':[0.38, 0.56, 0.68, 0.42, 0.58, 0.71, 0.39, 0.57, 0.69]} )

如果您不反对在 df2 中创建列(如果有问题,您可以在最后删除它们),您可以生成两列shapevd通过拆分列vd_combo

df2[['shape','vd']] = df2['vd_combo'].str.split('_',expand=True)

然后你可以创建一个condition你将使用的函数apply例如:

def condition( row, df2):
   # row will be a row of df1 in apply
   # here you select only the rows of df2 with your conditions on shape and value
   df_select = df2[(df2['shape'] == row['shape']) & (row['vd_pct'] <= df2['combo_value'])]
   # if empty (your condition not met) then return vd4
   if df_select.empty:
       return 'vd4'
   # if your condition met, then return the value of 'vd' the smallest
   else:
       return df_select['vd'].iloc[0]

现在你可以创建你的专栏vd_typedf1有:

df1['vd_type'] = df1.apply( condition, args =([df2]), axis=1)

df1 就好像:

  shape  vd_pct vd_type
0    A2    0.78     vd4
1    A1    0.33     vd1
2    B1    0.48     vd2
3    B1    0.38     vd1
4    A2    0.59     vd3

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

返回条件逻辑postgres的单独查询

Pandas 使用来自单独数据帧的值在数据帧中搜索子字符串

如果包含特定子字符串,则替换基于 Pandas 数据帧中的列值

如果整个字符串包含基于字典键的pandas数据帧中的子字符串,请替换整个字符串

如何基于唯一的字符串值将pandas数据帧拆分为多个数据帧而不进行聚合

如何在另一个数据帧 python pandas 中的多列上使用条件逻辑在数据帧中创建一列?

基于字符串的R数据帧的条件变异

Pandas 数据帧上的部分字符串合并

在pandas数据帧中将float转换为字符串

加快pandas数据帧中字符串的整数编码

通过在 pandas / python 中重命名基于条件的数据帧的单独行

使用条件逻辑从pandas df创建多个列表

基于多条件pandas的Groupby聚合

如何返回包含逻辑列的数据帧,这些逻辑列表示列中是否出现字符串?

基于字符串过滤聚合 R 中的数据帧

基于数据帧的字符串的模糊匹配

基于子字符串合并数据帧-python

根据条件熊猫过滤数据帧(字符串拆分)

循环R数据帧以生成条件字符串

在Pandas数据框中使用文本字符串数据进行条件数据选择

带条件的 Pandas groupy 字符串

基于使用字典值对其他数据框字段的多条件测试,使用字典键填充 Pandas 字段

如何使用Java为字符串编写条件逻辑

条件逻辑字符串的解析和正则表达式

空手道-带字符串和数组的条件逻辑

条件逻辑反应原生“文本字符串必须在 <Text> 组件中呈现。”

如何在整个Pandas数据帧中搜索字符串,并获取包含该字符串的列的名称?

在pandas数据帧中的字符串之后添加空格:TypeError:字符串索引必须为整数

从默认部分中的 numpy 条件逻辑返回列值