当多个选项为真时,如何创建此列以返回列表?

2024-09-28 05:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在这个例子中,我想为每一排购买最高速度的汽车,因为它有刹车(如果两者都没有刹车,它们同样有吸引力)

import pandas as pd

ExampleOfWhatIHave = {'Car51-speed':[1, 200, 19, 18],
        'Car52-speed':[20, 21, 19, 18],
       'Car51-brakes':[True, True, False, False],
        'Car52-brakes':[True, True, False, True]}

ExampleOfWhatINeed = {'Car51-speed':[1, 200, 19, 180],
        'Car52-speed':[20, 21, 19, 18],
       'Car51-brakes':[True, True, False, False],
        'Car52-brakes':[True, True, False, True],
           'Cartobuy': [['Car52'],['Car51'],['Car52','Car51'],['Car52']]}

# Create DataFrame
df = pd.DataFrame(ExampleOfWhatINeed)

请想象现实中汽车和变量的数量非常大(因此为51,52),因此“-”是可以用来循环它们的有意义的区别,而不是索引(我故意简化了它)


Tags: importfalsetruedataframe汽车速度例子pd
2条回答

另一种选择:

# Mask Brake Logic
brake_m = df.filter(regex='speed$').values * df.filter(regex='brakes$').values
# Get Unique Cars List
cars = np.unique(np.array(df.columns.str.split('-').tolist())[:, 0])
# Mask Speed Logic on Max
speed_m = pd.DataFrame((brake_m == np.amax(brake_m, axis=1)[:, None]),
                       columns=cars)
# Aggregate to List
df['cartobuy'] = speed_m.agg(lambda s: list(s.index[s]), axis=1)

df

   Car51-speed  Car52-speed  Car51-brakes  Car52-brakes        cartobuy
0            1           20          True          True         [Car52]
1          200           21          True          True         [Car51]
2           19           19         False         False  [Car51, Car52]
3           18           18         False          True         [Car52]

解释:

使用制动值屏蔽超速:

brake_m

[[  1  20]
 [200  21]
 [  0   0]
 [  0  18]]

然后根据最大值屏蔽超速值(如果有多个最大值,则保持)

cars

['Car51' 'Car52']

speed_m

   Car51  Car52
0  False   True
1   True  False
2   True   True
3  False   True

然后将行聚集到列表中并分配回数据帧:

speed_m.agg(lambda s: list(s.index[s]), axis=1)
0           [Car52]
1           [Car51]
2    [Car51, Car52]
3           [Car52]
dtype: object

完整的工作示例:

import numpy as np
import pandas as pd

ExampleOfWhatIHave = {'Car51-speed': [1, 200, 19, 18],
                      'Car52-speed': [20, 21, 19, 18],
                      'Car51-brakes': [True, True, False, False],
                      'Car52-brakes': [True, True, False, True]}
df = pd.DataFrame(ExampleOfWhatIHave)

brake_m = df.filter(regex='speed$').values * df.filter(regex='brakes$').values
cars = np.unique(np.array(df.columns.str.split('-').tolist())[:, 0])
speed_m = pd.DataFrame((brake_m == np.amax(brake_m, axis=1)[:, None]),
                       columns=cars)
df['cartobuy'] = speed_m.agg(lambda s: list(s.index[s]), axis=1)

print(df)

尝试:

speeds = df.filter(regex=r"speed$")
mask = df.filter(regex=r"brakes$")

x = speeds.mul(mask.values)
x["max"] = x.max(axis=1)
df["cartobuy"] = x.apply(
    lambda x: [
        i.split("-")[0] for i, val in zip(x.index, x[:-1]) if val == x["max"]
    ],
    axis=1,
)
print(df)

印刷品:

   Car51-speed  Car52-speed  Car51-brakes  Car52-brakes        Cartobuy
0            1           20          True          True         [Car52]
1          200           21          True          True         [Car51]
2           19           19         False         False  [Car52, Car51]
3          180           18         False          True         [Car52]

相关问题 更多 >

    热门问题