将 nan 值替换为前几个月的数据

发布于 2025-01-09 15:29:11 字数 1017 浏览 1 评论 0原文

我有一个数据框,如下所示。该 DataFrame 包含 NAN 值。我想用上个月的 DataFrame 中较早的非 nan 值替换 nan 值:

date (y-d-m)  | value 
 2022-01-01   | 1  
 2022-02-01   | 2 
 2022-03-01   | 3     
 2022-04-01   | 4  
 ...
 2022-01-02   | nan  
 2022-02-02   | nan
 2022-03-02   | nan
 2022-04-02   | nan
 ... 
 2022-01-03   | nan  
 2022-02-03   | nan
 2022-03-03   | nan
 2022-04-03   | nan

期望的结果

date (y-d-m)  | value 
 2022-01-01   | 1  
 2022-02-01   | 2 
 2022-03-01   | 3     
 2022-04-01   | 4  
 ...
 2022-01-02   | 1  
 2022-02-02   | 2
 2022-03-02   | 3
 2022-04-02   | 4
 ... 
 2022-01-03   | 1  
 2022-02-03   | 2
 2022-03-03   | 3
 2022-04-03   | 4

数据:

{'date (y-d-m)': ['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
                  '2022-01-02', '2022-02-02', '2022-03-02', '2022-04-02',
                  '2022-01-03', '2022-02-03', '2022-03-03', '2022-04-03'],
 'value': [1.0, 2.0, 3.0, 4.0, nan, nan, nan, nan, nan, nan, nan, nan]}

I have a DataFrame as follows. This DataFrame contains NAN values. I want to replace nan values with the earlier non nan value in my DataFrame from previous month(s):

date (y-d-m)  | value 
 2022-01-01   | 1  
 2022-02-01   | 2 
 2022-03-01   | 3     
 2022-04-01   | 4  
 ...
 2022-01-02   | nan  
 2022-02-02   | nan
 2022-03-02   | nan
 2022-04-02   | nan
 ... 
 2022-01-03   | nan  
 2022-02-03   | nan
 2022-03-03   | nan
 2022-04-03   | nan

Desired outcome

date (y-d-m)  | value 
 2022-01-01   | 1  
 2022-02-01   | 2 
 2022-03-01   | 3     
 2022-04-01   | 4  
 ...
 2022-01-02   | 1  
 2022-02-02   | 2
 2022-03-02   | 3
 2022-04-02   | 4
 ... 
 2022-01-03   | 1  
 2022-02-03   | 2
 2022-03-03   | 3
 2022-04-03   | 4

Data:

{'date (y-d-m)': ['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
                  '2022-01-02', '2022-02-02', '2022-03-02', '2022-04-02',
                  '2022-01-03', '2022-02-03', '2022-03-03', '2022-04-03'],
 'value': [1.0, 2.0, 3.0, 4.0, nan, nan, nan, nan, nan, nan, nan, nan]}

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

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

发布评论

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

评论(1

美人骨 2025-01-16 15:29:11

您可以将“date (ydm)”列转换为日期时间;然后groupby“day”并用ffill向前填充(前几个月同一天的值):

df['date (y-d-m)'] = pd.to_datetime(df['date (y-d-m)'], format='%Y-%d-%m')
df['value'] = df.groupby(df['date (y-d-m)'].dt.day)['value'].ffill()

输出:

   date (y-d-m)  value
0    2022-01-01    1.0
1    2022-01-02    2.0
2    2022-01-03    3.0
3    2022-01-04    4.0
4    2022-02-01    1.0
5    2022-02-02    2.0
6    2022-02-03    3.0
7    2022-02-04    4.0
8    2022-03-01    1.0
9    2022-03-02    2.0
10   2022-03-03    3.0
11   2022-03-04    4.0

You could convert "date (y-d-m)" column to datetime; then groupby "day" and forward fill with ffill (values from previous months' same day):

df['date (y-d-m)'] = pd.to_datetime(df['date (y-d-m)'], format='%Y-%d-%m')
df['value'] = df.groupby(df['date (y-d-m)'].dt.day)['value'].ffill()

Output:

   date (y-d-m)  value
0    2022-01-01    1.0
1    2022-01-02    2.0
2    2022-01-03    3.0
3    2022-01-04    4.0
4    2022-02-01    1.0
5    2022-02-02    2.0
6    2022-02-03    3.0
7    2022-02-04    4.0
8    2022-03-01    1.0
9    2022-03-02    2.0
10   2022-03-03    3.0
11   2022-03-04    4.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文