如何合并年份和月份列并添加日期

huangapple go评论118阅读模式
英文:

how to combine year and month column and add a date

问题

我有一个包含月份和年份列的DataFrame

```python
A = [2, 3, 4]
B = [2013, 2014, 2015]
df = pd.DataFrame({
    'A': A,
    'B': B,
})

我想要添加每个月的结束日期,并生成一个新列,如下所示:

A = [2, 3, 4]
B = [2013, 2014, 2015]
C = [2013-2-28, 2014-3-31, 2015-4-30]
df = pd.DataFrame({
    'A': A, 
    'B': B, 
    'assessDate': C,
})
英文:

I have a DataFrame with a column of months and a column of years:

A = [2, 3, 4]
B = [2013, 2014, 2015]
df = pd.DataFrame({
    'A': A,
    'B': B,
})

I want to add the end date of each month and generate a new column like this:

A = [2, 3, 4]
B = [2013, 2014, 2015]
C = [2013-2-28, 2014-3-31, 2015-4-30]
df = pd.DataFrame({
    'A': A, 
    'B': B, 
    'assessDate': C,
})

答案1

得分: 1

你可以使用calendar.monthrange(year, month)函数,它会返回一个元组,其中第一个元素是月份的第一天,第二个元素是给定年份中该月的天数。

代码:

import calendar

A = [2, 3, 4, 6, 8, 9, 10, 11]
B = [2013, 2014, 2015, 2019, 2020, 2021, 2022, 2023]
C = [f"{year}-{month}-{calendar.monthrange(year, month)[1]}" for year, month in zip(B, A)]
df = pd.DataFrame({'A': A, 'B': B, 'assessDate': C})

输出:

    A     B  assessDate
0   2  2013   2013-2-28
1   3  2014   2014-3-31
2   4  2015   2015-4-30
3   6  2019   2019-6-30
4   8  2020   2020-8-31
5   9  2021   2021-9-30
6  10  2022   2022-10-31
7  11  2023   2023-11-30
英文:

You can make use of calendar.monthrange(year, month), which returns a tuple where the first element is the first day of the month and the second is the number of days in the month in the given year.

Code:

import calendar

A = [2, 3, 4, 6, 8, 9, 10, 11]
B = [2013, 2014, 2015, 2019, 2020, 2021, 2022, 2023]
C = [f"{year}-{month}-{calendar.monthrange(year, month)[1]}" for year, month in zip(B, A)]
df = pd.DataFrame({'A': A, 'B': B, 'assessDate': C})

Output:

    A     B  assessDate
0   2  2013   2013-2-28
1   3  2014   2014-3-31
2   4  2015   2015-4-30
3   6  2019   2019-6-30
4   8  2020   2020-8-31
5   9  2021   2021-9-30
6  10  2022  2022-10-31
7  11  2023  2023-11-30

答案2

得分: 0

建议使用 pd.to_datetime() 将它们转换为月初的日期时间。

然后,Pandas 允许您在该日期时间上添加一个偏移,表示月初和月末之间的差异。

df['assessDate'] = pd.to_datetime(dict(year=df['B'], month=df['A'], day=1)) + pd.offsets.MonthEnd(0)
英文:

My suggestion would be to use pd.to_datetime() to turn those into a datetime for the first of the month.

Pandas then allows you to add an offset to that datetime representing the difference between the start and end of the month.

df['assessDate'] = pd.to_datetime(dict(year=df['B'], month=df['A'], day=1)) + pd.offsets.MonthEnd(0)

huangapple
  • 本文由 发表于 2023年3月7日 00:55:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定