尝试在 Pandas Dataframe 上运行函数时出现 TypeError

Posted

技术标签:

【中文标题】尝试在 Pandas Dataframe 上运行函数时出现 TypeError【英文标题】:TypeError when trying to run function on Pandas Dataframe 【发布时间】:2021-10-14 16:56:04 【问题描述】:

我有一个名为 month_values 的 pandas 数据框,其值如下:

Area Thrpt_A Thrpt_B Thrpt_C Thrpt_D
Galle 9581.539079 6790.664649 8340.489900 5940.886763
Colombo 5948.006563 4245.619220 7084.931812 3915.933362

数据类型是

Area             object
Thrpt_A          float64
Thrpt_B          float64
Thrpt_C          float64
Thrpt_D          float64

我已经定义了一个带有嵌套 if-else 的函数以应用于此数据帧,如下所示:

def mark_func(df):
    if (df['Area'] == "Colombo"):
        if (df['Thrpt_A'] >= 8000):
            return 'A'
        elif (df['Thrpt_A'] < (8000)):
            if (df['Thrpt_A'] >= df['Thrpt_B']) and (['Thrpt_A'] >= df['Thrpt_C']) and (['Thrpt_A'] >= df['Thrpt_D']):
                return 'A'
            elif (df['Thrpt_B'] >= df['Thrpt_C']) and (df['Thrpt_B'] >= df['Thrpt_D']):
                return 'B'
            elif (df['Thrpt_C'] >= df['Thrpt_B']) and (df['Thrpt_C'] >= df['Thrpt_D']):
                return 'C'
            elif (df['Thrpt_D'] >= df['Thrpt_B']) and (df['Thrpt_D'] >= df['Thrpt_C']):
                return 'D'
    else:
        if (df['Thrpt_A'] >= 4000):
            return 'A'
        elif (df['Thrpt_A'] < (4000)):
            if (df['Thrpt_A'] >= df['Thrpt_B']) and (['Thrpt_A'] >= df['Thrpt_C']) and (['Thrpt_A'] >= df['Thrpt_D']):
                return 'A'
            elif (df['Thrpt_B'] >= df['Thrpt_C']) and (df['Thrpt_B'] >= df['Thrpt_D']):
                return 'B'
            elif (df['Thrpt_C'] >= df['Thrpt_B']) and (df['Thrpt_C'] >= df['Thrpt_D']):
                return 'C'
            elif (df['Thrpt_D'] >= df['Thrpt_B']) and (df['Thrpt_D'] >= df['Thrpt_C']):
                return 'D'

我期待的输出如下:

Area Thrpt_A Thrpt_B Thrpt_C Thrpt_D Mark
Galle 9581.539079 6790.664649 8340.489900 5940.886763 A
Colombo 5948.006563 4245.619220 7084.931812 3915.933362 C

但是,当我使用以下代码行应用该函数时:

month_values['Mark'] = month_values.apply(mark_func, axis=1)

我收到以下错误:

TypeError: ("'>=' not supported between instances of 'list' and 'float'", 'occurred at index 279')

我不确定我在这里做错了什么。如有任何解决问题的建议,将不胜感激。

【问题讨论】:

【参考方案1】:

这部分代码:

... and (['Thrpt_A'] >= df['Thrpt_C']) and (['Thrpt_A'] >= df['Thrpt_D']):

应该是:

... and (df['Thrpt_A'] >= df['Thrpt_C']) and (df['Thrpt_A'] >= df['Thrpt_D']):

注意df

【讨论】:

以上是关于尝试在 Pandas Dataframe 上运行函数时出现 TypeError的主要内容,如果未能解决你的问题,请参考以下文章

在 pandas DataFrame 中的滚动窗口上对数据进行排名

pandas编写自定义函数使用apply函数应用自定义函数基于Series数据生成新的dataframe

pandas 中的大而持久的 DataFrame

将 Pandas DataFrame 切片成新的 DataFrame

使用多索引在 pandas DataFrame 上设置值

pandas DataFrame“没有要绘制的数字数据”错误