在Pandas中创建假期布尔列

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

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)

huangapple
  • 本文由 发表于 2023年8月8日 23:23:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860998.html
匿名

发表评论

匿名网友

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

确定