音乐会四分位数为每个ID的每月数据,而在季度的第一个月之前填写其他列

发布于 2025-02-11 15:34:54 字数 1077 浏览 4 评论 0原文

我试图通过填充以前的行值填充其他列来将pandas中每个唯一ID的每个唯一ID转换为一个月。我已经看过这一个 pandas:添加几个月的数据只有一个ID(对于多个ID不起作用),如果我们有多个ID,我们该怎么做?例如,我有这样的数据

import numpy as np
import pandas as pd
index = [0,1,2,3]
id = pd.Series([1,1,2,2],index= index)
price = pd.Series([20,41,61,68],index= index)
date_month = pd.Series(['2021-01','2021-04','2021-01','2021-04'],index= index)
df = pd.DataFrame(id,columns = ["id"])
df["price"] =price
df['date_month'] = date_month
df

,但我希望输出像

index = [0,1,2,3,4,5, 6, 7,8,9,10,11]
id = pd.Series([1,1,1,1,1,1,2, 2, 2,2,2,2],index= index)
price = pd.Series([20,20,20, 41,41,41, 61,61, 61, 68,68,68],index= index)
date_month = pd.Series(['2021-01', '2021-02','2021-03', '2021-04', '2021-05','2021-06','2021-01', '2021-02', '2021-03','2021-04', '2021-05','2021-06'],index= index)
df = pd.DataFrame(id,columns = ["id"])
df["price"] =price
df['date_month'] = date_month
df 

FYI一样,还有其他列,我们希望按每个季度的第一个月的值填充。

I am trying to convert quartiles into month for each unique id in pandas by filling other columns with previous row values. I have seen this one Pandas: Add data for missing months, but it's for only one id(it doesn't work for multiple id), how can we do the same thing if we have multiple id? For instance I have a data like this

import numpy as np
import pandas as pd
index = [0,1,2,3]
id = pd.Series([1,1,2,2],index= index)
price = pd.Series([20,41,61,68],index= index)
date_month = pd.Series(['2021-01','2021-04','2021-01','2021-04'],index= index)
df = pd.DataFrame(id,columns = ["id"])
df["price"] =price
df['date_month'] = date_month
df

But I want the output be like

index = [0,1,2,3,4,5, 6, 7,8,9,10,11]
id = pd.Series([1,1,1,1,1,1,2, 2, 2,2,2,2],index= index)
price = pd.Series([20,20,20, 41,41,41, 61,61, 61, 68,68,68],index= index)
date_month = pd.Series(['2021-01', '2021-02','2021-03', '2021-04', '2021-05','2021-06','2021-01', '2021-02', '2021-03','2021-04', '2021-05','2021-06'],index= index)
df = pd.DataFrame(id,columns = ["id"])
df["price"] =price
df['date_month'] = date_month
df 

FYI, there are other columns too, which we want fill by the value of the first month of the each quarter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦欢 2025-02-18 15:34:55

首先,创建一个日期范围数据框架,其中包含从最小月到最大月份的几个月。

import datetime

date_month = pd.to_datetime(df["date_month"])
min_date = date_month.min()
max_date = date_month.max() + pd.tseries.offsets.QuarterEnd()
date_range_df = pd.DataFrame(pd.date_range(min_date, max_date, freq="M"), columns=["date"])
date_range_df

date_range_df 创建一个季度的地图数据

def quarter2month(quarter):
    return (quarter -1) * 3 + 1

quarter_month_map_df = date_range_df.assign(
    date_month = date_range_df["date"].map(lambda x:datetime.datetime.strftime(x,"%Y-%m")),
    date_quarter = date_range_df["date"].map(lambda x:f"{x.year}-{quarter2month(x.quarter):02}"),
).drop("date",axis=1)

quarter_month_map_df

 date_month date_quarter
0 2021-01 2021-01
1 2021-02 2021-01
2 2021-03 2021-01
3 2021-04 2021-04
4 2021-05 2021-04
5 2021-06 2021-04

然后,基于

pd.merge(df.rename({"date_month": "date_quarter"}, axis=1),
     quarter_month_map_df, on='date_quarter', how="left")

First, create a date range dataframe containing the months from the smallest month to the largest month.

import datetime

date_month = pd.to_datetime(df["date_month"])
min_date = date_month.min()
max_date = date_month.max() + pd.tseries.offsets.QuarterEnd()
date_range_df = pd.DataFrame(pd.date_range(min_date, max_date, freq="M"), columns=["date"])
date_range_df

Then, create a quarter month map dataframe based on date_range_df

def quarter2month(quarter):
    return (quarter -1) * 3 + 1

quarter_month_map_df = date_range_df.assign(
    date_month = date_range_df["date"].map(lambda x:datetime.datetime.strftime(x,"%Y-%m")),
    date_quarter = date_range_df["date"].map(lambda x:f"{x.year}-{quarter2month(x.quarter):02}"),
).drop("date",axis=1)

quarter_month_map_df

The reuslt will be

 date_month date_quarter
0 2021-01 2021-01
1 2021-02 2021-01
2 2021-03 2021-01
3 2021-04 2021-04
4 2021-05 2021-04
5 2021-06 2021-04

Finally, you can merge the quarter_month_map_df with original df

pd.merge(df.rename({"date_month": "date_quarter"}, axis=1),
     quarter_month_map_df, on='date_quarter', how="left")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文