如何在Python中在特定日期为一个国家分配一个季节?

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

How to assign a season to a country on a particular day in Python?

问题

我正在处理一个类似这样的大数据集 -

如何在Python中在特定日期为一个国家分配一个季节?

我的目标是创建一个名为"season"的新列,该列包含每个国家每天的季节。为此,我必须检查国家是否位于北半球或南半球,然后根据以下条件为它们分配季节 -

如果国家位于北半球 -

季节 = 春季,如果月份是三月、四月、五月

季节 = 夏季,如果月份是六月、七月、八月

季节 = 秋季,如果月份是九月、十月、十一月

季节 = 冬季,如果月份是十二月、一月、二月

如果国家位于南半球 -

季节 = 春季,如果月份是九月、十月、十一月

季节 = 夏季,如果月份是十二月、一月、二月

季节 = 秋季,如果月份是三月、四月、五月

季节 = 冬季,如果月份是六月、七月、八月

我有两个名为north_hem_list和south_hem_list的列表,其中包含了所有北半球和南半球的国家。

我尝试通过从日期中找到月份,创建一个名为"season"的新列,然后尝试遍历数据集以分配季节来解决这个问题。我试图通过创建一个新的月份列,然后使用iterrows和iteritems来遍历数据框,但它没有起作用。

非常感谢任何形式的帮助。

英文:

I am working with a big dataset that looks like this -

如何在Python中在特定日期为一个国家分配一个季节?

My goal is to create a new column called season, which has the season for each country on each day. To do so I have to check if the country is in the northern or southern hemisphere, then assign them the season based on this criteria -

> If Country is Northern Hemisphere -
>
> Season = Spring if month is March, April, May
>
> Season = Summer if month is June, July, August
>
> Season = Fall if month is September, October, November
>
> Season = Winter if month is December, January, February

> If Country is Southern Hemisphere -
>
> Season = Spring if month is September, October, November
>
> Season = Summer if month is December, January, February
>
> Season = Fall if month is March, April, May
>
> Season = Winter if month is June, July, August

I am given the list of all nothern and southern hemisphere countries in the form of 2 lists named north_hem_list and south_hem_list.

I tried to solve this by finding the month from the date, create a new column called season, then try to iterate through the dataset to assign season. I tried to solve this by creating a new columns with months, then using iterrows and iteritems to iterate through the dataframe. However, it is not working.

Would appreciate any and all help with this.

答案1

得分: 3

以下是翻译好的代码部分:

# 创建一个函数来根据半球和月份分配季节
def get_season(row):
    date = pd.to_datetime(row['Date'])
    month = date.month
    hemisphere = ''
    if row['Country'] in north_hem_list:
        hemisphere = 'Northern Hemisphere'
    elif row['Country'] in south_hem_list:
        hemisphere = 'Southern Hemisphere'
    for season, months in season_dict[hemisphere].items():
        if month in months:
            return season

# 将函数应用于数据框以创建季节列
df['Season'] = df.apply(get_season, axis=1)
英文:

You can try this solution (The input df was not provided so I couldn't show the result..)

season_dict = {
    'Northern Hemisphere': {
        'Spring': [3, 4, 5],
        'Summer': [6, 7, 8],
        'Fall': [9, 10, 11],
        'Winter': [12, 1, 2]
    },
    'Southern Hemisphere': {
        'Spring': [9, 10, 11],
        'Summer': [12, 1, 2],
        'Fall': [3, 4, 5],
        'Winter': [6, 7, 8]
    }
}

# create a function to assign season based on hemisphere and month
def get_season(row):
    date = pd.to_datetime(row['Date'])
    month = date.month
    hemisphere = ''
    if row['Country'] in north_hem_list:
        hemisphere = 'Northern Hemisphere'
    elif row['Country'] in south_hem_list:
        hemisphere = 'Southern Hemisphere'
    for season, months in season_dict[hemisphere].items():
        if month in months:
            return season

# apply the function to the dataframe to create the season column
df['Season'] = df.apply(get_season, axis=1)

答案2

得分: 2

您可以使用以下代码:

# 示例
df = pd.DataFrame({'Code': ['AFG', 'ZWE'], 'Day': ['2020-02-24', '2023-02-01']})
df['Day'] = pd.to_datetime(df['Day'])

# 基于Code的检测
north_hem_list = ['AFG']
south_hem_list = ['ZWE']

# 季节
north_seasons = ['Winter', 'Spring', 'Summer', 'Fall']
south_seasons = ['Summer', 'Fall', 'Winter', 'Spring']

season = (df['Day'].dt.month % 12 + 3) // 3 - 1
df['Season'] = np.where(df['Code'].isin(north_hem_list),
                        np.array(north_seasons)[season],
                        np.array(south_seasons)[season])

输出:

>>> df
  Code        Day  Season
0  AFG 2020-02-24  Winter
1  ZWE 2023-02-01  Summer

不要翻译代码部分。

英文:

You can use:

# Sample
df = pd.DataFrame({'Code': ['AFG', 'ZWE'], 'Day': ['2020-02-24', '2023-02-01']})
df['Day'] = pd.to_datetime(df['Day'])

# Detection based on Code
north_hem_list = ['AFG']
south_hem_list = ['ZWE']

# Seasons
north_seasons = ['Winter', 'Spring', 'Summer', 'Fall']
south_seasons = ['Summer', 'Fall', 'Winter', 'Spring']

season = (df['Day'].dt.month % 12 + 3) // 3 - 1
df['Season'] = np.where(df['Code'].isin(north_hem_list),
                        np.array(north_seasons)[season],
                        np.array(south_seasons)[season])

Output:

>>> df
  Code        Day  Season
0  AFG 2020-02-24  Winter
1  ZWE 2023-02-01  Summer

huangapple
  • 本文由 发表于 2023年3月9日 15:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75681547.html
匿名

发表评论

匿名网友

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

确定