我有df如下所示,我正在尝试逐行生成一个状态列,其中包含特定的日期。
但是我得到了有关DF对象的信息,而不是行的值。
代码:

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)预期结果:

发布于 2022-04-24 20:46:35
您可以使用字符串连接:
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")。所以
choices = ["It was delivered to the customer on the date " + df["delivery_date"].astype('string'),
"delivery forecast for " + df["estimated_date"].astype('string')]输出:
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 发布于 2022-04-24 21:25:19
由于Series对象包含许多值,而且str.format需要一个单数值,所以考虑string与的串联。
...
# 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)https://stackoverflow.com/questions/71991039
复制相似问题