【问题标题】:How to execute permuted operations of itertools on a Python DataFrame?如何在 Python DataFrame 上执行 itertools 的置换操作?
【发布时间】:2022-01-26 16:43:12
【问题描述】:

我在 Python 中有以下数据框 test

test = pd.DataFrame({'id':['F8510004123','A3100002543','Z3510002123'],
                     'product':['retail','retail','others'],
                     'type':['E','E','D'],
                     'quantity':[25,34,150],
                     'nro_ope':[2,3,26],
                     'payments':[[1030.97,1030.97,584.91],[1610.74,1610.74,1610.74,1610.74,1611.14],[1007.52,1007.52,1007.52,1007.52,500,500,852.95]]
                     })
test.dtypes

用表格表示:

id product types quantity nro_ope payments
F8510004123 retail E 25 2 1030.97,1030.97,584.91
A3100002543 retail E 34 3 1610.74,1610.74,1610.74,1610.74,1611.14
Z3510002123 others D 150 26 1007.52,1007.52,1007.52,1007.52,500,500,852.95

根据数据类型:

test.dtypes

输出[449]:

id 对象

产品对象

类型对象

数量 int64

支付对象

dtype: 对象

我想使用库 itertools 对数据框 testpayments 列的每个元素执行以下置换操作Python:

  payment_column_1 = [1030.97,1030.97,584.91]
  payment_column_2 = [1610.74,1610.74,1610.74,1610.74,1611.14]
  payment_column_3 = [1007.52,1007.52,1007.52,1007.52,500,500,852.95]
 
 
  from itertools import permutations
  permuted_operation_1 = [round(abs(a/b -1),3) for a,b in permutations(payment_column_1,2)]
  permuted_operation_2 = [round(abs(a/b -1),3) for a,b in permutations(payment_column_2,2)]
  permuted_operation_3 = [round(abs(a/b -1),3) for a,b in permutations(payment_column_3,2)]

输出:

print(permuted_operation_1)

[0.0, 0.763, 0.0, 0.763, 0.433, 0.433]

print(permuted_operation_2)

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

print(permuted_operation_3)

[0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.504, 0.504, 0.504, 0.504, 0.0, 0.414、0.504、0.504、0.504、0.504、0.0、0.414、0.153、0.153、0.153、0.153、0.706、0.706]

但是,如您所见,当我需要来自同一数据框 test 创建一个新列 var_payments 时,我从列表中应用此置换操作插入数组,其中包含数据框具有的每一行的操作结果,如下表所示:

id product types quantity nro_ope payments var_payments
F8510004123 retail E 25 2 {1030.97,1030.97,584.91} {0.0, 0.763, 0.0, 0.763, 0.433, 0.433}
A3100002543 retail E 34 3 {1610.74,1610.74,1610.74,1610.74,1611.14} {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}
Z3510002123 others D 150 26 {1007.52,1007.52,1007.52,1007.52,500,500,852.95} {0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.504, 0.504, 0.504, 0.504, 0.0, 0.414, 0.504, 0.504, 0.504, 0.504, 0.0, 0.414, 0.153, 0.153, 0.153, 0.153, 0.706, 0.706}

我在以下方面取得了进展,但显然是错误的:

test['var_payments'] = test['payments'].apply( lambda r:round(abs(a/b -1),3) for a,b in 
permutations (r,2))

输出:

NameError: name 'r' 未定义

最后,我需要创建列flag,它基本上遍历了新创建的列var_payments的元素,如果发现至少有两个元素大于或等于0.05,则为Yes,否则为No

id product types quantity nro_ope payments var_payments flag
F8510004123 retail E 25 2 {1030.97,1030.97,584.91} {0.0, 0.763, 0.0, 0.763, 0.433, 0.433} Yes
A3100002543 retail E 34 3 {1610.74,1610.74,1610.74,1610.74,1611.14} {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0} No
Z3510002123 others D 150 26 {1007.52,1007.52,1007.52,1007.52,500,500,852.95} {0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.0, 0.0, 0.0, 1.015, 1.015, 0.181, 0.504, 0.504, 0.504, 0.504, 0.0, 0.414, 0.504, 0.504, 0.504, 0.504, 0.0, 0.414, 0.153, 0.153, 0.153, 0.153, 0.706, 0.706} Yes

我该怎么做?

我很关注你们的cmets。

感谢支持。

【问题讨论】:

  • df = pd.DataFrame({"col1": [1, 2, 3, etc..]}) 的形式将您的数据帧作为代码提供,以便我们可以将其粘贴到我们的 IDE 中

标签: python pandas loops foreach itertools


【解决方案1】:

我是通过在 python 中创建函数得到的:

test= pd.DataFrame({'id':['F8510004123','A3100002543','Z3510002123'],
                     'product':['retail','retail','others'],
                     'type':['E','E','D'],
                     'quantity':[25,34,150],
                     'nro_ope':[2,3,26],
                     'payments':[[1030.97,1030.97,584.91],[1610.74,1610.74,1610.74,1610.74,1611.14],[1007.52,1007.52,1007.52,1007.52,500,500,852.95]]
                     })

;

def var_payments (x) :
    variation  = [round(abs(a/b -1),3) for a,b in permutations(x,2)]
    return variation 

;

def count_var_pay (x) :
    count = 0
    for element in x:
        if element >= 0.05 :
            count += 1
        else:
             0                    
    return count

;

def flag_var_payments (x) : 
    if x >= 2 :
        return 'Yes'
    else : 
        return 'No'

;

test['var_payments'] = test.payments.apply(lambda x:var_payments(x))

test['count_p'] = test.var_payments.apply(lambda x:count_var_pay(x))

test['flag'] = test.count_p.apply(lambda x:flag_var_payments(x))

【讨论】:

    猜你喜欢
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多