使用表格的两个不同列的Python函数

时间:2019-09-03 02:05:35

标签: python dataframe

我有一个表格,其中一列是出生日期,另一列是学生的出生月份。我需要将其转换为他们的十二生肖。

我在互联网上发现了一个功能,可以将出生的月和日转换成十二生肖,但是需要输入。如果它来自表的两列怎么办?

def signo(table): #from https://www.w3resource.com/python-exercises/python-conditional-exercise-38.php
    month = int(table.iloc[:,1])
    day = int(table.iloc[:,2])
    astro_sign = 0
    if month == 12:
        astro_sign = 'sagittarius' if (day < 22) else 'capricorn'
    elif month == 1:
        astro_sign = 'capricorn' if (day < 20) else 'aquarius'
    elif month == 2:
        astro_sign = 'aquarius' if (day < 19) else 'pisces'
    elif month == 3:
        astro_sign = 'pisces' if (day < 21) else 'aries'
    elif month == 4:
        astro_sign = 'aries' if (day < 20) else 'taurus'
    elif month == 5:
        astro_sign = 'taurus' if (day < 21) else 'gemini'
    elif month == 6:
        astro_sign = 'gemini' if (day < 21) else 'cancer'
    elif month == 7:
        astro_sign = 'cancer' if (day < 23) else 'leo'
    elif month == 8:
        astro_sign = 'leo' if (day < 23) else 'virgo'
    elif month == 9:
        astro_sign = 'virgo' if (day < 23) else 'libra'
    elif month == 10:
        astro_sign = 'libra' if (day < 23) else 'scorpio'
    elif month == 11:
        astro_sign = 'scorpio' if (day < 22) else 'sagittarius'
    return astro_sign

我尝试使用iloc或命名该列,但是它不起作用(说实话,我不知道它是否应该起作用)。 使用iloc时得到的是:

NameError: ("name 'table' is not defined", 'occurred at index CO_CURSO')

2 个答案:

答案 0 :(得分:0)

您可以修改表以在DataFrame的单行上工作,然后使用apply来转换整个数据集,而不是尝试使用函数signo中的整个表:

首先让我们创建一些示例数据:

import numpy as np
import pandas as pd

np.random.seed(28)
df = pd.DataFrame(
    data={
        "birth_month": np.random.randint(1, 12, 10),
        "birth_day": np.random.randint(1, 30, 10)
    }
)
print(df)

输出:

   birth_month  birth_day
0            2         24
1           10         13
2            6         13
3            7          6
4            5          9
5            1         25
6            4         19
7            8         12
8            1         29
9            4         25

然后,我们修改函数signo以逐行工作:

def signo(row):
    month = int(row[0])
    day = int(row[1])
    astro_sign = 0
    if month == 12:
        astro_sign = 'sagittarius' if (day < 22) else 'capricorn'
    elif month == 1:
        astro_sign = 'capricorn' if (day < 20) else 'aquarius'
    elif month == 2:
        astro_sign = 'aquarius' if (day < 19) else 'pisces'
    elif month == 3:
        astro_sign = 'pisces' if (day < 21) else 'aries'
    elif month == 4:
        astro_sign = 'aries' if (day < 20) else 'taurus'
    elif month == 5:
        astro_sign = 'taurus' if (day < 21) else 'gemini'
    elif month == 6:
        astro_sign = 'gemini' if (day < 21) else 'cancer'
    elif month == 7:
        astro_sign = 'cancer' if (day < 23) else 'leo'
    elif month == 8:
        astro_sign = 'leo' if (day < 23) else 'virgo'
    elif month == 9:
        astro_sign = 'virgo' if (day < 23) else 'libra'
    elif month == 10:
        astro_sign = 'libra' if (day < 23) else 'scorpio'
    elif month == 11:
        astro_sign = 'scorpio' if (day < 22) else 'sagittarius'
    return astro_sign

最后应用该函数并在DataFrame中创建一个新列:

df['sign'] = df.apply(signo, axis=1)
print(df)

输出:

   birth_month  birth_day      sign
0            2         24    pisces
1           10         13     libra
2            6         13    gemini
3            7          6    cancer
4            5          9    taurus
5            1         25  aquarius
6            4         19     aries
7            8         12       leo
8            1         29  aquarius
9            4         25    taurus

答案 1 :(得分:0)

我假设当您说表时,您的意思是python数据框。

您可以使用iterrows方法遍历每一行

import pandas as pd
data = [{'month': 1, 'day': 10}, {'month': 4, 'day': 11},{'month': 5, 'day': 17},{'month': 11, 'day': 20}]
df = pd.DataFrame(data)
for index, row in df.iterrows():
    month = row['month']
    day = row['day']

休息,然后按照逻辑将其转换成十二生肖