行中的过滤器值包含用于进一步计算的特定字符串

问题描述

这是我的数据框:

In [2]: fruits = pd.DataFrame({"apple_price": [100,100,200,500,600,200],...:                        "cherry_price": [2,3,1,2,1],...:                        "banana_price": [2,4,5,3],...:                        "prices": ["apple_price","apple_price","cherry_price","banana_price",...:  "banana_price","apple_price"],...:                        "price_fruits": [100,200]})

In [3]: fruits
Out[3]:
   apple_price  cherry_price  banana_price        prices  price_fruits
0          100             2             2   apple_price           100
1          100             3             4   apple_price           100
2          200             1             5  cherry_price           100
3          500             0             2  banana_price             2
4          100             2             3  cherry_price             3
5          600             1             5  banana_price             5
6          200             1             3   apple_price           200

基本上,苹果 ["apple_price"] 的价格必须除以 100(因为 apple_price 中的价格以美分而不是欧元为单位),而其他水果的价格应保持不变。

所以这将是我期望的输出

In [5]: fruits_ad
Out[5]:
   apple_price  cherry_price  banana_price        prices  price_fruits  pr_fruits_adjusted
0          100             2             2   apple_price           100                   1
1          100             3             4   apple_price           100                   1
2          200             1             5  cherry_price           100                   1
3          500             0             2  banana_price             2                   2
4          100             2             3  cherry_price             3                   3
5          600             1             5  banana_price             5                   5
6          200             1             3   apple_price           200                   2

谢谢!

巧 拉

解决方法

根据条件对价格进行调整,可以这样做:

vec

您可以根据自己的要求更改条件。这里我写了一个条件,如果价格大于或等于 100,则应用调整。 fruits['pr_fruits_adjusted'] = np.where((fruits.price_fruits >=100),fruits['price_fruits']/100,fruits['price_fruits']).astype('int32')

,

对不起,伙计们,我匆忙弄乱了我的示例数据框。

所以你:

你从这里开始...

In [2]: fruits = pd.DataFrame({"Variety": ["apple_price","cherry_price","banana_price","apple_price","banana_price"
   ...: ],...:                        "Price": [100,2,3,200,3]})

In [3]: fruits
Out[3]:
        Variety  Price
0   apple_price    100
1  cherry_price      2
2  banana_price      3
3   apple_price    200
4  banana_price      3

这应该是预期的输出:

In [4]: fruits_adj = pd.DataFrame({"Variety": ["apple_price","banana_pr
   ...: ice"],^M
   ...:                            "Price": [100,3],^M
   ...:                            "Price_Adj": [1,3]})

In [5]: fruits_adj
Out[5]:
        Variety  Price  Price_Adj
0   apple_price    100          1
1  cherry_price      2          2
2  banana_price      3          3
3   apple_price    200          2
4  banana_price      3          3

简而言之,我正在寻找的代码将字符串拆分为“Variety”列并检查我是否包含“apple”,如果此条件 == True,您应该将 apple_price 的价格除以 100 否则取“价格”列中的相应水果。

所以这将是我的尝试,也许你有更优雅的东西:

In [6]: chk = fruits_adj['Variety'].str.contains(r'apple',na=True)

In [7]: fruits.loc[chk,"Price_Adj"] = fruits["Price"] / 100

In [8]: fruits.loc[~chk,"Price_Adj"] = fruits["Price"]

In [9]: fruits
Out[9]:
        Variety  Price  Price_Adj
0   apple_price    100        1.0
1  cherry_price      2        2.0
2  banana_price      3        3.0
3   apple_price    200        2.0
4  banana_price      3        3.0

干杯 拉