根据条件将值插入数据框列

时间:2018-11-11 21:32:05

标签: python pandas numpy

如何重写以下代码的最后两行,以使最后一行不会覆盖倒数第二行?

期望的结果是,“颜色”列中将根据满足以下条件输入“粉红色”或“橙色”值:“ KOM”或“前10名”。

import pandas as pd
import numpy as np

def contains_BO(seg_effs):  
        # check if segment efforts for activity contain any best overall effort
        for eff in seg_effs:
            rank = eff['kom_rank']
            if rank != None:
                if rank == 1:
                    return "KOM"
                else:
                    return "Top 10"

activities = pd.read_pickle('strava.pk1') 
activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'KOM', "orange", "grey")
activities['color'] = np.where(activities['segment_efforts'].map(contains_BO) == 'Top 10', "pink", "grey")

1 个答案:

答案 0 :(得分:0)

您使用类似这样的内容:

import pandas as pd

df = pd.DataFrame({"a": range(4), "b": ["x", "x", "y", "y"]})
df

   a  b
0  0  x
1  1  x
2  2  y
3  3  y

# assign 5 to rows of "a" where "b" == "x"
df.loc[df["b"] == "x", "a"] = 5
df

   a  b
0  5  x
1  5  x
2  2  y
3  3  y

或者,您可以根据值的字典来创建新列:

df["val"] = df["b"].map({"x": 5, "y": 6})

df

   a  b  val
0  5  x    5
1  5  x    5
2  2  y    6
3  3  y    6

map还支持功能,如果您需要更复杂的逻辑。