首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在.format中使用np.select?

如何在.format中使用np.select?
EN

Stack Overflow用户
提问于 2022-04-24 18:07:13
回答 2查看 34关注 0票数 0

我有df如下所示,我正在尝试逐行生成一个状态列,其中包含特定的日期。

但是我得到了有关DF对象的信息,而不是行的值。

代码:

代码语言:javascript
复制
import pandas as pd
import numpy as np

data = {'local':  ['client', 'hub'],
    'delivery_date':  [pd.to_datetime('2022-04-24'), 'null'],
    'estimated_date':  [pd.to_datetime('2022-04-24'), pd.to_datetime('2022-04-26')],
    'max_date': ['delivery_date', 'estimated_date']
    }

 df = pd.DataFrame(data)
 cond = [(df["max_date"] == "delivery_date")  & (df["local"] == "client"),
         (df["max_date"] == "estimated_date") & (df["local"] == "hub")]

 choices = ["It was delivered to the customer on the date {}".format(df["delivery_date"]),
            "delivery forecast for {}".format(df["estimated_date"])]

 df["status"] = np.select(cond, choices, default = np.nan)

预期结果:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-24 20:46:35

您可以使用字符串连接:

代码语言:javascript
复制
choices = ["It was delivered to the customer on the date " + df["delivery_date"].astype(str),
            "delivery forecast for " + df["estimated_date"].astype(str)]
df["status"] = np.select(cond, choices, default = np.nan)

As @Parfait注意到,如果您有pandas>=1.0.0,请为引入的StringDtype使用astype("string")。所以

代码语言:javascript
复制
choices = ["It was delivered to the customer on the date " + df["delivery_date"].astype('string'),
            "delivery forecast for " + df["estimated_date"].astype('string')]

输出:

代码语言:javascript
复制
    local        delivery_date estimated_date        max_date                                             status  
0  client  2022-04-24 00:00:00     2022-04-24   delivery_date  It was delivered to the customer on the date 2...   
1     hub                 null     2022-04-26  estimated_date                   delivery forecast for 2022-04-26  
票数 2
EN

Stack Overflow用户

发布于 2022-04-24 21:25:19

由于Series对象包含许多值,而且str.format需要一个单数值,所以考虑string与的串联。

代码语言:javascript
复制
...

# ADD HELPER COLUMNS
df["delivery_note"] = "It was delivered to the customer on the date "
df["forecast_note"] = "delivery forecast for " 

choices = [
    df["delivery_note"].str.cat(df["delivery_date"]),
    df["forecast_note"].str.cat(df["estimated_date"])
]

df["status"] = np.select(cond, choices, default = np.nan)

# REMOVE HELPER COLUMNS
df.drop(["delivery_note", "forecast_note"], axis="columns", inplace=True)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71991039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档