
我有一个 pandas 数据框,如下所示,其中详细说明了对某个区域的其他调用:
| commsdate | area | day0 incremental | day1 incremental | day2 incremental |
|---|---|---|---|---|
| 01/01/24 | sales | 43 | 36 | 29 |
| 01/01/24 | service | 85 | 74 | 66 |
| 02/01/24 | sales | 56 | 42 | 31 |
| 02/01/24 | service | 73 | 62 | 49 |
| 03/01/24 | sales | 48 | 32 | 24 |
| 03/01/24 | service | 67 | 58 | 46 |
我正在尝试按日期计算收到的电话数量,因此 1 月 1 日收到的销售电话将是该日期的 day0_incremental (43),1 月 2 日将是 1 月 2 日的 day0 加上 1 月 1 日的 day1 (36+) 56) 和 1 月 3 日将是 1 月 3 日的 day0 加上 1 月 2 日的 day1 加上 1 月 1 日的 day2 (48+42+29),产生以下数据框:
| CallDate | Sales | Service |
|---|---|---|
| 01/01/24 | 43 | 85 |
| 02/01/24 | 92 | 147 |
| 03/01/24 | 119 | 195 |
| 04/01/24 | 63 | 107 |
| 05/01/24 | 24 | 46 |
我已经成功地为第二个表创建了数据框的外壳,在区域列下没有值,但不知道接下来的步骤:
df['commsdate'] = pd.to_datetime(df['commsdate'], format='%d/%m/%y')
areaunique = df['area'].unique().tolist()
from datetime import timedelta
calldate = pd.date_range(start=min(df['commsdate']), end=max(df['commsdate'])+timedelta(days=6), freq='d')
data = {area: [] for area in areaunique}
dfnew = pd.dataframe(data)
dfnew['calldate'] = calldate
dfnew = dfnew.melt(id_vars=['calldate'], var_name='area')
dfnew = dfnew.pivot(index='calldate', columns='area', values='value')
dfnew = dfnew.reset_index()
dfnew = dfnew[['calldate'] + areaunique]我已经开始编写 for 循环,但我只做到了这一点:
Gyb2b V1.01免费版可终身使用,是一款功能强大的B2B电子商务应用软件。该软件不仅更新和修改了V1.0相关功能,更是采用了目前互联网上最流行的LAMP组合(Linux+Apache+Mysql+PHP)开发完成,模板技术实现了界面与代码的有效分离,用户可以快速地在此基础上编译模板;提供B2B电子商务应用最常见的求购、供应、商品、公司库、行业资讯、商圈、资信认证、在线交易、交易评分、留言、搜
0
for i in range(1,len(areaunique)+1):
dfnew.columns(i) =df['commsdate'] = pd.to_datetime(df['commsdate'], dayfirst=true)
tmp = df.pivot(index='commsdate', columns='area')
out = (tmp['day0 incremental']
.add(tmp['day1 incremental'].shift(freq='1d'), fill_value=0)
.add(tmp['day2 incremental'].shift(freq='2d'), fill_value=0)
.reset_index().rename_axis(columns=none)
)或者,使用从 dayx … 字符串中提取的数字以编程方式使用 functools.reduce:
from functools import reduce
import re
reg = re.compile(r'day(\d+)')
df['commsdate'] = pd.to_datetime(df['commsdate'], dayfirst=true)
tmp = df.pivot(index='commsdate', columns='area')
out = reduce(lambda a,b: a.add(b, fill_value=0),
(tmp[d].shift(freq=f'{reg.search(d).group(1)}d') for d in
tmp.columns.get_level_values(0).unique())
).reset_index().rename_axis(columns=none)输出:
CommsDate Sales Service 0 2024-01-01 43.0 85.0 1 2024-01-02 92.0 147.0 2 2024-01-03 119.0 195.0 3 2024-01-04 63.0 107.0 4 2024-01-05 24.0 46.0
以上就是与pandas有条件合并的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号