【问题标题】:Pandas: Newbie question on compare and (re)calculate fields with pandasPandas:关于使用 pandas 比较和(重新)计算字段的新手问题
【发布时间】:2021-04-01 07:29:24
【问题描述】:

我需要做的是比较 csv 文件中连续的 2 个字段:

数据如下所示:

store;ean;price;retail_price;quantity
001;0888721396226;200;200;2
001;0888721396233;200;159;2
001;2194384654084;299;259;7
001;2194384654091;199.95;199.95;8

如果“price”等于“retail_price”,则字段retail_price 必须减少给定的百分比值,例如-10%

所以在示例数据中,第一行和最后一行应改为 180 和 179,955

我对 pandas 完全陌生,在阅读“入门”部分后,我没有找到任何可以设置的内容......

所以任何帮助或提示(只要指出我的方向,然后我会自己搞定)表示感谢,

亲切的问候!

【问题讨论】:

    标签: pandas compare


    【解决方案1】:

    使用Series.eq 比较两个值,如果相同的多个retail_price 通过0.9 否则不在numpy.where

    mask = df['price'].eq(df['retail_price'])
    df['retail_price'] = np.where(mask, df['retail_price'].mul(0.9), df['retail_price'])
    print (df)
       store            ean   price  retail_price  quantity
    0      1   888721396226  200.00       180.000         2
    1      1   888721396233  200.00       159.000         2
    2      1  2194384654084  299.00       259.000         7
    3      1  2194384654091  199.95       179.955         8
    

    或者您可以将DataFrame.loc 用于0.9 的多个仅匹配行:

    mask = df['price'].eq(df['retail_price'])
    df.loc[mask, 'retail_price'] *= 0.9
    
    #working like
    df.loc[mask, 'retail_price'] = df.loc[mask, 'retail_price'] * 0.9
    

    编辑:对于不匹配掩码的过滤器行(掩码中带有 False),请使用:

    df2 = df[~mask].copy()
    print (df2)
       store            ean  price  retail_price  quantity
    1      1   888721396233  200.0         159.0         2
    2      1  2194384654084  299.0         259.0         7
    
    
    print (mask)
    0     True
    1    False
    2    False
    3     True
    dtype: bool
    

    【讨论】:

    • 亲爱的 Jezrael,我设法让它工作了。太感谢了 !但是现在我的输出有点问题,因为输出现在也将行号作为文件中的一个新的前导列,例如store ean price retail_price 数量 0 1 888721396233 200.00 200.00 2 1 1 2194384654084 299.00 299.00 7 该文件不能包含此列 btw 我的完整代码现在是:df['retail_price'] = np.where(mask, df['retail_price']。 mul(mulvalue).round(2), df['retail_price']) print (df) df.to_csv('output.csv')
    • @MarkusPöschl - 所以需要df.to_csv(file, index=False) 吗?
    • 再次感谢提示:'code' df.to_csv('output.csv', columns=['store','ean','price','retail_price','quantity'] ,sep=';') 成功了 :-)
    • index=False -> 没有改变输出中的任何内容,但是...
    • 非常感谢!我现在还添加了dtype={'store': object,'ean': object},因此它不会更改 EAN 和 Store-ID(删除前导零)-> 现在完美了!!祝您度过愉快的一周:感谢您的耐心和帮助!真的很感激!
    【解决方案2】:

    这是我的代码:

    import pandas as pd
    import numpy as np
    import sys
    
    with open('prozente.txt', 'r') as f:    #create multiplicator from static value in File "prozente.txt"
        prozente = int(f.readline())
    mulvalue = 1-(prozente/100)    
    df = pd.read_csv('1.csv', sep=';', header=1, names=['store','ean','price','retail_price','quantity'])
    mask = df['price'].eq(df['retail_price'])
    df['retail_price'] = np.where(mask, df['retail_price'].mul(mulvalue).round(2), df['retail_price'])
    df2 = df[~mask].copy()
    df.to_csv('output.csv', columns=['store','ean','price','retail_price','quantity'],sep=';', index=False)
    print(df)
    print(df2)
    

    将此用作 1.csv:

    store;ean;price;retail_price;quantity
    001;0888721396226;200;200;2
    001;0888721396233;200;159;2
    001;2194384654084;299;259;7
    001;2194384654091;199.95;199.95;8
    

    文件“prozente.txt”的内容是

    25 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2017-02-03
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多