简体   繁体   English

创建月份和年份列中没有天数的日期 pandas dataframe

[英]Create Dates without days present in month & year columns pandas dataframe

  1. I want to Create a new column, called 'Month_Year', using lambda function. Its values should be: '01-01-2020' for January, 2020 and '01-02-2020' for February 2020 and so on.我想使用 lambda function 创建一个名为“Month_Year”的新列。其值应为:2020 年 1 月为“01-01-2020”,2020 年 2 月为“01-02-2020”,依此类推。 The data set contains only month:{jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec} obj dtype.数据集仅包含 month:{jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec} obj dtype。 I have to convert this month_names to month_numbers.我必须将这个 month_names 转换为 month_numbers。 dataset also has year column with data from 2020 - 2022 int dtype.数据集还有年份列,其中包含 2020 - 2022 int dtype 的数据。 I want to create dates for this dataset using Month and Year.我想使用月和年为此数据集创建日期。 enter image description here在此处输入图像描述
{year = pd.to_datetime(df1.Year, format='%Y').dt.year
df1['Month'] = pd.to_datetime(df1.Month, format='%b').dt.month}


cal = calendar.Calendar()
for i in cal.itermonthdays3(year=year, month=month):
    print(i)
/// Error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
{2nd way}`
def date_iter(year, month):
    for i in range(1, calendar.monthrange(year, month) + 1):
        yield date(year, month, i)

for d in date_iter(year, month):
    print(d)
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How can I create dates column based on month and year Column?如何根据月份和年份列创建日期列?

Try:尝试:

df["Month_Year"] = pd.to_datetime(df["Month"] + "-" + df["Year"].astype(str))
print(df)

Prints:印刷:

   Month  Year Month_Year
0    Jan  2020 2020-01-01
1    Feb  2021 2021-02-01
2    Mar  2022 2022-03-01
3    Apr  2020 2020-04-01
4    May  2021 2021-05-01
5    Jun  2022 2022-06-01
6    Jul  2020 2020-07-01
7    Aug  2021 2021-08-01
8    Sep  2022 2022-09-01
9    Oct  2020 2020-10-01
10   Nov  2021 2021-11-01
11   Dec  2022 2022-12-01

Input dataframe:输入 dataframe:

   Month  Year
0    Jan  2020
1    Feb  2021
2    Mar  2022
3    Apr  2020
4    May  2021
5    Jun  2022
6    Jul  2020
7    Aug  2021
8    Sep  2022
9    Oct  2020
10   Nov  2021
11   Dec  2022

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas 数据框:对于一年中的每个月,如果月份不存在,则将当月最后一天的日期添加到索引中,或者删除重复项 - Python Pandas dataframe: For each month of the year, add the date with last day in the month to an index if month not present, or remove duplicates 在 DataFrame pandas 中为日期和年份创建布尔列 - Create boolean columns in DataFrame pandas for date and year PANDAS粗略日期(月/年) - PANDAS coarse dates (month/year) 如何为 Pandas 中的 2 天数据从年、日、小时和分钟列(无月列)创建日期时间 object? - How to create a datetime object from Year, Day, Hour and Minute columns (without month column) for 2 day data in Pandas? 按年将 DataFrame 组织成列并按日月索引 - PYTHON - PANDAS - Organize DataFrame into columns by year and index by day-month - PYTHON - PANDAS 将月-年格式拆分为 pandas dataframe 中的单独列? - Splitting the Month-Year format into seperate columns in a pandas dataframe? 如何创建一个年月系列用作熊猫数据框中的索引? - How to create a year-month series to use as an index in a pandas dataframe? 在 x 轴上创建带有年份月份的 pandas dataframe 的方面 plot - Create facet plot of pandas dataframe with month of year on x-axis 按 pandas dataframe 中的月份和年份分组 - Group By Month and Year in pandas dataframe 按年和月分组熊猫数据框 - Group Pandas Dataframe by Year and Month
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM