英文:
Creating Holiday Boolean Column in Pandas
问题
我有一个如下结构的Pandas DataFrame:
date | country |
---|---|
2017-01-01 | 西班牙 |
2019-10-23 | 巴西 |
我想要添加一个名为"is_holiday"的布尔列(1或0)到DataFrame中。这一列应该是一个指示器,用于判断日期(索引)是否是"country"列所指定的国家的国定假日。
我尝试了很多不同的方法,但是我无法解决这个问题。
有人可以帮助我吗?
提前感谢!
英文:
I have a Pandas DataFrame with the structure below:
date | country |
---|---|
2017-01-01 | Spain |
2019-10-23 | Brazil |
And I want to add a boolean column (1 or 0) "is_holiday" to it. This column should be an indicator to wether the date (index) is a national holiday in the specific country of the "country" column or not.
I've tried a lot of differents approachs, but I can't manage to work it out.
Can anybody help me?
Thanks in advance!
答案1
得分: 1
尝试使用holidays库。
import pandas as pd
import holidays
# 示例数据框
data = {'date': ['2017-01-01', '2019-10-23'],
'country': ['Spain', 'Brazil']}
df = pd.DataFrame(data)
# 检查日期是否为特定国家的假日的函数
def is_holiday(row):
country_holidays = holidays.CountryHoliday(row['country'])
return int(row['date'] in country_holidays)
# 应用该函数创建'is_holiday'列
df['is_holiday'] = df.apply(is_holiday, axis=1)
print(df)
英文:
Try using the holidays library.
import pandas as pd
import holidays
# Sample DataFrame
data = {'date': ['2017-01-01', '2019-10-23'],
'country': ['Spain', 'Brazil']}
df = pd.DataFrame(data)
# Function to check if date is a holiday in a specific country
def is_holiday(row):
country_holidays = holidays.CountryHoliday(row['country'])
return int(row['date'] in country_holidays)
# Applying the function to create 'is_holiday' column
df['is_holiday'] = df.apply(is_holiday, axis=1)
print(df)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论