pandas 创建对每个字符串匹配进行计数的新列

pu82cl6c  于 2023-03-28  发布在  其他
关注(0)|答案(3)|浏览(88)

我的目标是创建一个基于多个字符串匹配的新列。这个新列将为事件列上的每个匹配返回1。

Date        Event
0 2022-11-01        Breakfast
1 2022-11-01        Breakfast
2 2022-11-01        Lunch
3 2022-11-02        Breakfast
4 2022-11-02        Lunch

之后

Date        Event        Breakfast        Lunch
0 2022-11-01        Breakfast        1            null
1 2022-11-01        Breakfast        1            null
2 2022-11-01        Lunch            null         1
3 2022-11-02        Breakfast        1            null
4 2022-11-02        Lunch            null         1
polkgigr

polkgigr1#

您可以使用pd.get_dummies执行以下任务:

out = pd.concat([df, pd.get_dummies(df['Event'])], axis=1)
print(out)

图纸:

Date      Event  Breakfast  Lunch
0  2022-11-01  Breakfast          1      0
1  2022-11-01  Breakfast          1      0
2  2022-11-01      Lunch          0      1
3  2022-11-02  Breakfast          1      0
4  2022-11-02      Lunch          0      1

如果你想要None而不是0

out = out.replace({0: None})
print(out)

图纸:

Date      Event Breakfast Lunch
0  2022-11-01  Breakfast         1  None
1  2022-11-01  Breakfast         1  None
2  2022-11-01      Lunch      None     1
3  2022-11-02  Breakfast         1  None
4  2022-11-02      Lunch      None     1
laximzn5

laximzn52#

您也可以使用Map功能:

df['Breakfast'] = df.Event.map({'Breakfast': 1})
df['Lunch'] = df.Event.map({'Lunch': 1})
shyt4zoc

shyt4zoc3#

这很容易。

df = pd.DataFrame([["2022-11-01", "Breakfast"],
                  ["2022-11-01", "Breakfast"],
                  ["2022-11-01", "Lunch"],
                  ["2022-11-02", "Breakfast"],
                  ["2022-11-02", "Lunch"]], columns=["date", "event"])
溶液
df["temp"] = df["event"]
dummies = pd.get_dummies(df, prefix="", prefix_sep="", columns=["temp"])
输出
date        event       Breakfast   Lunch
0   2022-11-01  Breakfast   1           0
1   2022-11-01  Breakfast   1           0
2   2022-11-01  Lunch       0           1
3   2022-11-02  Breakfast   1           0
4   2022-11-02  Lunch       0           1

相关问题