如何根据输入年份在一年中的日子生成数据框架?

发布于 2025-01-19 07:50:12 字数 1073 浏览 0 评论 0原文

我想生成一个数据框,根据指定年份将一年中的天数附加到第一列。我该怎么做?我正在使用 pandas date_range 模块。

这是我尝试过的:

#Import modules
import pandas as pd
import numpy as np
import datetime as dt

#Specify the year
year = 1976

#Create dataframe
df = pd.Series(pd.date_range(year, periods=365, freq='D'))

print(df)

结果:

0     1970-01-01 00:00:00.000001976
1     1970-01-02 00:00:00.000001976
2     1970-01-03 00:00:00.000001976
3     1970-01-04 00:00:00.000001976
4     1970-01-05 00:00:00.000001976
                   ...             
360   1970-12-27 00:00:00.000001976
361   1970-12-28 00:00:00.000001976
362   1970-12-29 00:00:00.000001976
363   1970-12-30 00:00:00.000001976
364   1970-12-31 00:00:00.000001976
Length: 365, dtype: datetime64[ns]

这里的年份是错误的,我需要它是 1976 年。此外,我需要的只是一个“一年中的某一天”列,其中的行数与该中的天数相对应年(这将考虑闰年)。我该如何解决这个问题?

输出应该是一个如下所示的数据框(它应该一直延伸到一年的最后一天):

d = {
    'year': [1976, 1976, 1976, 1976, 1976, 1976],
    'day of the year': [1, 2, 3, 4, 5, 6]
}
df1 = pd.DataFrame(data=d)
df1

I would like to generate a dataframe with the days of the year appended to the first column based on a specified year. How can I do this? I am using the pandas date_range module.

Here is what I have tried:

#Import modules
import pandas as pd
import numpy as np
import datetime as dt

#Specify the year
year = 1976

#Create dataframe
df = pd.Series(pd.date_range(year, periods=365, freq='D'))

print(df)

The result:

0     1970-01-01 00:00:00.000001976
1     1970-01-02 00:00:00.000001976
2     1970-01-03 00:00:00.000001976
3     1970-01-04 00:00:00.000001976
4     1970-01-05 00:00:00.000001976
                   ...             
360   1970-12-27 00:00:00.000001976
361   1970-12-28 00:00:00.000001976
362   1970-12-29 00:00:00.000001976
363   1970-12-30 00:00:00.000001976
364   1970-12-31 00:00:00.000001976
Length: 365, dtype: datetime64[ns]

The year is wrong here, I need it to be 1976. Additionally, all I need is a "Day of the Year" column with the number of rows corresponding to the number of days in the year (this would account for leap years). How can I fix this?

The output should be a dataframe that looks like this (it should extend all the way to the last day of the year):

d = {
    'year': [1976, 1976, 1976, 1976, 1976, 1976],
    'day of the year': [1, 2, 3, 4, 5, 6]
}
df1 = pd.DataFrame(data=d)
df1

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

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

发布评论

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

评论(1

娜些时光,永不杰束 2025-01-26 07:50:12
year = 1976
dates = pd.Series(pd.date_range(str(year) + "-01-01", str(year) + "-12-31", freq="D"))
days = dates.diff().astype("timedelta64[D]").fillna(1).cumsum()
df = pd.DataFrame({"year": dates.dt.year, "days": days})
df = df.set_index(dates)
print(df)
#             year   days
# 1976-01-01  1976    1.0
# 1976-01-02  1976    2.0
# 1976-01-03  1976    3.0
# 1976-01-04  1976    4.0
# 1976-01-05  1976    5.0
# ...          ...    ...
# 1976-12-27  1976  362.0
# 1976-12-28  1976  363.0
# 1976-12-29  1976  364.0
# 1976-12-30  1976  365.0
# 1976-12-31  1976  366.0

# [366 rows x 2 columns]

或者

import calendar

year = 1976

n_days = 366 if calendar.isleap(year) else 365
df = pd.DataFrame({"year": year,
                   "days": range(1, n_days + 1)})
year = 1976
dates = pd.Series(pd.date_range(str(year) + "-01-01", str(year) + "-12-31", freq="D"))
days = dates.diff().astype("timedelta64[D]").fillna(1).cumsum()
df = pd.DataFrame({"year": dates.dt.year, "days": days})
df = df.set_index(dates)
print(df)
#             year   days
# 1976-01-01  1976    1.0
# 1976-01-02  1976    2.0
# 1976-01-03  1976    3.0
# 1976-01-04  1976    4.0
# 1976-01-05  1976    5.0
# ...          ...    ...
# 1976-12-27  1976  362.0
# 1976-12-28  1976  363.0
# 1976-12-29  1976  364.0
# 1976-12-30  1976  365.0
# 1976-12-31  1976  366.0

# [366 rows x 2 columns]

Or

import calendar

year = 1976

n_days = 366 if calendar.isleap(year) else 365
df = pd.DataFrame({"year": year,
                   "days": range(1, n_days + 1)})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文